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?
I had the same problem that UserInfo was nil (on iOS 9.2). The problem was that the search result wasn't the latest NSUserActivity which I have tapped (older one with no UserInfo). So I renamed the title to clearly identify the correct search result.
Maybe this helps someone, too.
It was a problem with beta 3 that was causing this error. Everything is working fine as of beta 5.
Hi could you check in your continueUserActivity snippet:
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.
I noticed (in the simulator) that you can set either:
But not both! If you set both an webpageURL and userInfo with requiredUserInfoKeys, your activity won't even show up in spotlight on iOS. I think this is because Apple assumes if you set the webpage URL that you support universal links and thus don't need any additional user info.
It is, however, OK to set both the webpage and just userInfo, but in your app delegate you won't receive the userInfo.
See this question: NSUserActivity handoff not working for custom data
userInfo is non-null if you follow this guideline from Apple:
The doc does say that setting webpageURL requires that requiredUserInfoKeys also be set. However (a) handoff works fine without requiredUserInfoKeys, as long as the 'userActivityWillSave' set userInfo, and (b) webURL is an iOS 8 parameter, whereas requiredUserInfoKeys is iOS 9. So the delegate method allows handoff to be supported in iOS 8 / Xcode 6.
I'm working in iOS 9 beta 5, and another way that
userInfo
may be nil within theapplication:continueUserActivity:restorationHandler:
is if you have also setNSUserActivity
'swebPageURL
property. If you set thewebPageURL
property and would likeuserInfo
to not be nil, then you will need to include the appropriate keys inNSUserActivity
'srequiredUserInfoKeys
property. This took me forever to figure out, but shame on me for missing this in the docs.