无法恢复 NSUserActivity 的 userInfo
Can't restore NSUserActivity's userInfo
我有一个应用程序,您可以在其中找到我们大学的员工,并通过 e-mail 与他们联系。当您找到某人并查看详细信息时,将创建一个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);
returns 正确的 userInfo
。
当我在 Spotlight 中找到条目(正确显示所有相关数据)并单击它时,应用程序将打开并调用 - (BOOL)application:continueUserActivity:restorationHandler:
并且 userActivity
包含正确的标题和类型。但是 userInfo
和 requiredUserInfoKeys
都是空的。
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler {
NSLog(@"Activity: %@\nInfo: %@", userActivity.activityType, userActivity.requiredUserInfoKeys);
return YES;
}
我不明白,我哪里弄错了。我已经阅读 ,但没有帮助。
PS: _displayedPerson.personID
是一个 NSNumber
而 [_displayedPerson completeName]
returns 是一个 NSString
我在 Apple 开发者论坛中找到了答案。 updateUserActivityState:
需要覆盖。
- (void)updateUserActivityState:(NSUserActivity *)userActivity {
[userActivity addUserInfoEntriesFromDictionary:@{@"id" : _displayedPerson.personID, @"fullName" : [_displayedPerson completeName]}];
}
我不知道为什么我必须这样做,但它有效。希望,这也会对其他人有所帮助。
我有一个应用程序,您可以在其中找到我们大学的员工,并通过 e-mail 与他们联系。当您找到某人并查看详细信息时,将创建一个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);
returns 正确的 userInfo
。
当我在 Spotlight 中找到条目(正确显示所有相关数据)并单击它时,应用程序将打开并调用 - (BOOL)application:continueUserActivity:restorationHandler:
并且 userActivity
包含正确的标题和类型。但是 userInfo
和 requiredUserInfoKeys
都是空的。
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler {
NSLog(@"Activity: %@\nInfo: %@", userActivity.activityType, userActivity.requiredUserInfoKeys);
return YES;
}
我不明白,我哪里弄错了。我已经阅读
PS: _displayedPerson.personID
是一个 NSNumber
而 [_displayedPerson completeName]
returns 是一个 NSString
我在 Apple 开发者论坛中找到了答案。 updateUserActivityState:
需要覆盖。
- (void)updateUserActivityState:(NSUserActivity *)userActivity {
[userActivity addUserInfoEntriesFromDictionary:@{@"id" : _displayedPerson.personID, @"fullName" : [_displayedPerson completeName]}];
}
我不知道为什么我必须这样做,但它有效。希望,这也会对其他人有所帮助。