iosobjective-ccorespotlightnsuseractivity

Can't restore NSUserActivity's userInfo


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


Solution

  • 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.