Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 9 - NSUserActivity userinfo property showing null

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];  

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.

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

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;

}

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

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

like image 977
Jason Ngo Avatar asked Jul 09 '15 20:07

Jason Ngo


3 Answers

Hi could you check in your continueUserActivity snippet:

if ([userActivity.activityType isEqualToString:@"com.test.activity"])
{
      NSString *locationName = [[userActivity.userInfo objectForKey:@"location"] valueForKey: @"name"];
}

If it doesn't go there I assume that you have Core Spotlight integrated in your app and you get in Spotlight exactly by these elements from Core Spotlight.

Hope it was helpful.

P.S. At the moment(Xcode 7 beta 3) many developers can't get Search API through NSUserActivity to work, including me.

like image 111
Richard Mazkewich Avatar answered Nov 08 '22 21:11

Richard Mazkewich


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];
like image 11
markckim Avatar answered Nov 08 '22 20:11

markckim


It was a problem with beta 3 that was causing this error. Everything is working fine as of beta 5.

like image 1
Jason Ngo Avatar answered Nov 08 '22 22:11

Jason Ngo