3

I have an app where you can find employees at our university to contact them via e-mail. When you found someone and look at the detailed information, a NSUserActivity is created:

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")) {
        //Create NSUserActivity for the person
        NSUserActivity * activity = [[NSUserActivity alloc] initWithActivityType:CAUserActivityTypePerson];
        activity.title = [_displayedPerson completeName];
        activity.requiredUserInfoKeys = [NSSet setWithArray:@[@"id", @"fullName"]];
        activity.userInfo = [NSDictionary dictionaryWithObjects:@[_displayedPerson.personID, [_displayedPerson completeName]] forKeys:@[@"id", @"fullName"]];


        activity.keywords = [NSSet setWithObjects:[_displayedPerson completeName], [_displayedPerson institutionString], nil];

        CSSearchableItemAttributeSet * contentAttributeSet = [[CSSearchableItemAttributeSet alloc] init];
        contentAttributeSet.contentDescription = [_displayedPerson institutionString];

        activity.contentAttributeSet = contentAttributeSet;

        //make the activity active and searchable
        self.userActivity = activity;
        self.userActivity.eligibleForHandoff = NO;
        self.userActivity.eligibleForSearch = YES;
        self.userActivity.eligibleForPublicIndexing = NO;
        self.userActivity.needsSave = YES;
        [self.userActivity becomeCurrent];
        NSLog(@"UserActivity created\nInfo: %@", self.userActivity.userInfo);
    } else
        NSLog(@"Did not create NSUserActivity! iOS 9 required!");

While creating the UserActivity, NSLog(@"UserActivity created\nInfo: %@", self.userActivity.userInfo); returns the correct userInfo.

When I find the entry (displaying all relevant data correctly) in spotlight and click it, the app will be opened and - (BOOL)application:continueUserActivity:restorationHandler: is called and userActivity contains the correct title and type. But userInfo and requiredUserInfoKeys are both empty.

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler {

    NSLog(@"Activity: %@\nInfo: %@", userActivity.activityType, userActivity.requiredUserInfoKeys);

    return YES;
}

I don't get, where I made a mistake. I already read this, but it didn't help.

PS: _displayedPerson.personID is a NSNumber and [_displayedPerson completeName] returns a NSString

Community
  • 1
  • 1
FelixSFD
  • 6,052
  • 10
  • 43
  • 117

1 Answers1

2

I found an answer in the Apple Developer Forums. updateUserActivityState: needs to be overwritten.

- (void)updateUserActivityState:(NSUserActivity *)userActivity {
    [userActivity addUserInfoEntriesFromDictionary:@{@"id" : _displayedPerson.personID, @"fullName" : [_displayedPerson completeName]}];
}

I have no idea, why I had to do this, but it works. Hope, this will help other people, too.

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
  • If you don't want to use `delegate`, this is another property to use: `requiredUserInfoKeys`, the strings must be included in the keys used in the userInfo dictionary. – bubuxu Jan 16 '17 at 03:22