无法恢复NSUserActivity的userInfo

时间:2015-12-26 17:16:06

标签: ios objective-c corespotlight nsuseractivity

我有一个应用程序,您可以在我们的大学找到员工通过电子邮件与他们联系。当您找到某人并查看详细信息时,会创建NSUserActivity

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!");

在创建UserActivity时,NSLog(@"UserActivity created\nInfo: %@", self.userActivity.userInfo);会返回正确的userInfo

当我在聚光灯下找到该条目(正确显示所有相关数据)并单击它时,将打开该应用并调用- (BOOL)application:continueUserActivity:restorationHandler:并且userActivity包含正确的标题和类型。但userInforequiredUserInfoKeys都是空的。

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

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

    return YES;
}

我不知道,我犯了错误。我已经阅读了this,但它没有帮助。

PS:_displayedPerson.personIDNSNumber[_displayedPerson completeName]返回NSString

1 个答案:

答案 0 :(得分:2)

我在Apple Developer Forums中找到了答案。 c需要被覆盖。

updateUserActivityState:

我不知道,为什么我必须这样做,但它确实有效。希望,这也将有助于其他人。