iosxcodeios9user-activitycorespotlight

continueUserActivity not called from search closed app


I am trying to use core spotlight to open a view controller from the spotlight search results.

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

if(self.window.rootViewController){
    [self.window.rootViewController restoreUserActivityState:userActivity];
}

return YES;
}

This seems to work when the app is already running in background, however when it is closed and I tap on the spotlight search result it seems that this method gets not called and the behavior I get is that my application simply starts in the main interface.

Do you have any suggestion for making it work also when my app is closed? Is there a way to debug what is happening (since I need to run the app to get the debugger attached I don't know how to simulate the app opening from the search result)?.


Solution

  • Here it follows the complete answer following your advices.

    // In application: didFinishLaunchingWithOptions:
    NSDictionary *activityDic = [launchOptions objectForKey:UIApplicationLaunchOptionsUserActivityDictionaryKey];
    
    if (activityDic) {
        if(self.window.rootViewController){
            NSUserActivity * userActivity = [activityDic valueForKey:@"UIApplicationLaunchOptionsUserActivityKey"];
            [self.window.rootViewController restoreUserActivityState:userActivity];
        }
    }