iOS app launch due to NSUserActivity. How do access that object? iOS app launch due to NSUserActivity. How do access that object? ios ios

iOS app launch due to NSUserActivity. How do access that object?


You shouldn't be doing any of that. You should return YES from -(BOOL)application:didFinishLaunchingWithOptions:, then -(BOOL)application:continueUserActivity:restorationHandler will also be called. Apple docs say:

NO if the app cannot handle the URL resource or continue a user activity, otherwise return YES.


I used a UIAlertView to get a description of the launchOptions when coming from an NSUserActivity:

NSDictionary *userActivityDictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsUserActivityDictionaryKey];UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"alert"                                                    message:[userActivityDictionary description]                                                   delegate:nil                                          cancelButtonTitle:@"Okay"                                          otherButtonTitles:nil];[alertView show];

Apparently, this dictionary comes with a couple keys that I cannot seem to find documentation for:

UIApplicationLaunchOptionsUserActivityIdentifierKeyUIApplicationLaunchOptionsUserActivityKeyUIApplicationLaunchOptionsUserActivityTypeKey // there is documentation for this key

enter image description here

It seems the NSUserActivity object is inside this dictionary, but it may not be good from Apple's point of view (or it may break if Apple decides to change things), to try to access this object (e.g., possibly looping through the dictionary keys and looking for an NSUserActivity object).

**** Solution ****

EDIT: Here's some implementation on how to access the NSUserActivity object from launchOptions:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    NSDictionary *userActivityDictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsUserActivityDictionaryKey];    if (userActivityDictionary) {        [userActivityDictionary enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {                if ([obj isKindOfClass:[NSUserActivity class]]) {                    NSLog(@"found NSUserActivity object!");                }            }];    }}

Using this method, you don't need to know the value of the undocumented key, UIApplicationLaunchOptionsUserActivityKey.

If you're uncomfortable with the solution above, another method may be to catch the NSUserActivity object when your app enters this method,

- (BOOL)application:continueUserActivity:restorationHandler:

Then, set this user activity as a property of your AppDelegate for later retrieval.

Note: I was able to verify that the method above is called on a cold app launch while testing Core Spotlight (iOS 9 feature).


Swift answer:

// Handle universal link    if let activityDic = launchOptions?[UIApplicationLaunchOptionsUserActivityDictionaryKey] as? [NSObject : AnyObject],              activity = activityDic["UIApplicationLaunchOptionsUserActivityKey"] as? NSUserActivity {                  handleActivity(activity)    }


matomo