iOS 9 - NSUserActivity userinfo属性显示为null [英] iOS 9 - NSUserActivity userinfo property showing null

查看:504
本文介绍了iOS 9 - NSUserActivity userinfo属性显示为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于NSUserActivity的userInfo属性的快速问题。

I had a quick question about NSUserActivity's userInfo property.

NSString *activity = @"com.test.activity";  
NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:activity];  
userActivity.title = @"Test";  
userActivity.keywords = [NSSet setWithArray:@[@"Test"];  
userActivity.userInfo = @{@"location": location};  

userActivity.eligibleForSearch = YES;    
self.userActivity = userActivity;  
[self.userActivity becomeCurrent];  

我在视图控制器viewDidLoad()之一中实现了上述代码片段。当我的项目出现在聚光灯搜索中时,它会调用continueUserActivity委托方法。

I have the above snippet implemented in one of view controllers viewDidLoad(). When my item appears within spotlight search it calls the continueUserActivity delegate method.

我正在尝试访问userActivity.userInfo属性但它返回null,即使它已在上面设置。

I'm trying to access the userActivity.userInfo property but it's returning null even though it's been set above.

这是continueUserActivity片段:

Here's the continueUserActivity snippet:

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

NSString *locationName = [[userActivity.userInfo objectForKey:@"location"] valueForKey: @"name"];

// Do stuff with locationName

return NO;

}

编辑:我改变了location对象作为基本类型返回,但我仍然在委托方法上获取null。

I changed the location object to return as a primitive type but I'm still getting null on the delegate method.

iOS 9 beta 3中是否有其他人遇到此问题?

Is anyone else having this problem in iOS 9 beta 3?

推荐答案

我在iOS 9 beta 5中工作,另一种方式 userInfo 应用程序中可能为nil:continueUserActivity:restorationHandler:如果你还设置了 NSUserActivity webPageURL 属性。如果您设置 webPageURL 属性并希望 userInfo 不是nil,那么您需要包含相应的键在 NSUserActivity requiredUserInfoKeys 属性中。我花了很长时间才弄明白,但在文档中遗漏了这个让我感到羞耻。

I'm working in iOS 9 beta 5, and another way that userInfo may be nil within the application:continueUserActivity:restorationHandler: is if you have also set NSUserActivity's webPageURL property. If you set the webPageURL property and would like userInfo to not be nil, then you will need to include the appropriate keys in NSUserActivity's requiredUserInfoKeys property. This took me forever to figure out, but shame on me for missing this in the docs.

NSUserActivity *activity = [[NSUserActivity alloc] initWithActivityType:activityType];
activity.title = @"title";
activity.webpageURL = [NSURL URLWithString:webLinkUrlString];
activity.userInfo = @{@"id": webLinkUrlString, @"deep_link": deepLinkUrl};
// set requiredUserInfoKeys if you're also setting webPageURL !!
// otherwise, userInfo will be nil !!
activity.requiredUserInfoKeys = [NSSet setWithArrays:@[@"id", @"deep_link"]];
activity.eligibleForSearch = YES;
activity.eligibleForPublicIndexing = YES;
activity.contentAttributeSet = attributeSet;
activity.keywords = [NSSet setWithArray:keywordsArray];
self.activity = activity;
[self.activity becomeCurrent];

这篇关于iOS 9 - NSUserActivity userinfo属性显示为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆