IOS 9 - NSUserActivity userinfo property showing zero

I had a quick question about NSUserActivity user information properties.

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 viewDidLoad () controllers. When my item appears in the spotlight in search, it calls the continueUserActivity delegate method.

I am trying to access the userActivity.userInfo property, but it returns null even if it was 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 still get null in the delegate method.

Does anyone else have this problem in iOS 9 beta 3?

+8
ios objective-c ios9
source share
6 answers

I work in iOS 9 beta 5, and another way that userInfo can be zero in application:continueUserActivity:restorationHandler: is if you also set the NSUserActivity webPageURL property. If you set the webPageURL property and want userInfo not to be zero, you will need to include the appropriate keys in the NSUserActivity requiredUserInfoKeys property. It made me figure it out forever, but disgrace me for losing it in the documents.

 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]; 
+9
source share

Hi, you can check 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 built into your application, and you get to Spotlight with these elements from Core Spotlight.

Hope this was helpful.

PS At the moment ( Xcode 7 beta 3 ), many developers cannot get the Search API through NSUserActivity to work, including me.

+2
source share

This was a beta 3 issue that caused this error. Everything works fine with beta 5.

0
source share

See this question: NSUserActivity handover does not work for user data

userInfo is not null if you follow this Apple guide:

To effectively update the user data dictionary of activity objects, configure its delegate and set its needsSave property to YES when user information needs to be updated. At appropriate times, the Handoff calls the delegates userActivityWillSave: callback, and the delegate can update the activity status.

Doc says that installing webpageURL requires the necessary UserInfoKeys to be installed as well. However, (a) a handover works fine without mandatory UserInfoKeys if userActivityWillSave sets userInfo and (b) webURL is an iOS 8 parameter, whereas requiredUserInfoKeys is iOS 9. Thus, the delegate method allows for handover in iOS 8 / Xcode 6.

0
source share

I noticed (in the simulator) that you can set:

  • Webpage url
  • userInfo and requiredUserInfoKeys

But not both! If you installed both webpageURL and userInfo with requiredUserInfoKeys, your activity will not be displayed in the spotlight of iOS. I think this is due to the fact that Apple assumes that you set the URL of the web page to support universal links and therefore do not need additional information about the user.

However, it’s OK to set both a web page and just userInfo, but in application deletion you won’t get userInfo.

0
source share

I had the same problem that UserInfo was zero (on iOS 9.2). The problem was that the search result was not the last NSUserActivity I used (older, without UserInfo). So I renamed the title to clearly identify the correct search result.

Perhaps this also helps someone.

0
source share

All Articles