continueUserActivity未从搜索已关闭的应用程序调用

时间:2021-09-17 20:04:31

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)?.

当我的应用程序关闭时,您是否有任何建议让它工作?有没有办法调试正在发生的事情(因为我需要运行应用程序以连接调试器我不知道如何从搜索结果模拟应用程序打开)?

5 个解决方案

#1


31  

Niko,

first of all: there's a way to start your app from Xcode and not opening it immediately: open your scheme properties, go to the "run" section, and under "info", there's a switch that will help you to debug what's happening:

首先:有一种方法可以从Xcode启动你的应用程序而不是立即打开它:打开你的方案属性,转到“运行”部分,在“信息”下,有一个开关可以帮助你调试正在发生的事情:

"Wait for executable to be launched".

“等待可执行文件启动”。

If you activate this switch, you can launch the app from Xcode, Xcode will wait until the app is opened from search and then it will attach the debugger to it.

如果您激活此开关,您可以从Xcode启动应用程序,Xcode将等待从搜索打开应用程序,然后它将调试器附加到它。

Hope that helps!

希望有所帮助!

Ivan

#2


19  

If you are using the Facebook SDK, and in didfinishlaunching your are returning FBSDK, instead of plain text, and returning true at the end, it can cause problems hitting continueuseractivity.

如果你正在使用Facebook SDK,并且在didfinishlaunching你正在返回FBSDK,而不是纯文本,并在最后返回true,它可能会导致问题触及continueuseractivity。

After searching a lot, and trying different ways, I just had to return true and comment this:

经过大量搜索,并尝试不同的方式,我只需要返回true并对此进行评论:

FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)

FBSDKApplicationDelegate.sharedInstance()。application(application,didFinishLaunchingWithOptions:launchOptions)

#3


6  

didFinishLaunchingWithOptions needs to return YES so continueUserActivity will be called.

didFinishLaunchingWithOptions需要返回YES,因此将调用continueUserActivity。

Add to End of application: didFinishLaunchingWithOptions:

添加到应用程序结束:didFinishLaunchingWithOptions:

NSDictionary *activityDictionary = launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey];
if (activityDictionary) {
    NSUserActivity *userActivity = activityDictionary[UIApplicationLaunchOptionsUserActivityTypeKey];
    if (userActivity) {
        return YES;
    }
}

return NO;

#4


5  

If the app was closed, application: continueUserActivity: is not called. Instead, you get all the information launchOptions dictionnary in application: didFinishLaunchingWithOptions:

如果应用程序已关闭,则不会调用application:continueUserActivity:。相反,您将在应用程序中获取所有信息launchOptions dictionnary:didFinishLaunchingWithOptions:

// In application: didFinishLaunchingWithOptions:
NSDictionary *activityDic = [launchOptions objectForKey:UIApplicationLaunchOptionsUserActivityDictionaryKey];  
if (activityDic) {
    // Continue activity here
}

#5


3  

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

#1


31  

Niko,

first of all: there's a way to start your app from Xcode and not opening it immediately: open your scheme properties, go to the "run" section, and under "info", there's a switch that will help you to debug what's happening:

首先:有一种方法可以从Xcode启动你的应用程序而不是立即打开它:打开你的方案属性,转到“运行”部分,在“信息”下,有一个开关可以帮助你调试正在发生的事情:

"Wait for executable to be launched".

“等待可执行文件启动”。

If you activate this switch, you can launch the app from Xcode, Xcode will wait until the app is opened from search and then it will attach the debugger to it.

如果您激活此开关,您可以从Xcode启动应用程序,Xcode将等待从搜索打开应用程序,然后它将调试器附加到它。

Hope that helps!

希望有所帮助!

Ivan

#2


19  

If you are using the Facebook SDK, and in didfinishlaunching your are returning FBSDK, instead of plain text, and returning true at the end, it can cause problems hitting continueuseractivity.

如果你正在使用Facebook SDK,并且在didfinishlaunching你正在返回FBSDK,而不是纯文本,并在最后返回true,它可能会导致问题触及continueuseractivity。

After searching a lot, and trying different ways, I just had to return true and comment this:

经过大量搜索,并尝试不同的方式,我只需要返回true并对此进行评论:

FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)

FBSDKApplicationDelegate.sharedInstance()。application(application,didFinishLaunchingWithOptions:launchOptions)

#3


6  

didFinishLaunchingWithOptions needs to return YES so continueUserActivity will be called.

didFinishLaunchingWithOptions需要返回YES,因此将调用continueUserActivity。

Add to End of application: didFinishLaunchingWithOptions:

添加到应用程序结束:didFinishLaunchingWithOptions:

NSDictionary *activityDictionary = launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey];
if (activityDictionary) {
    NSUserActivity *userActivity = activityDictionary[UIApplicationLaunchOptionsUserActivityTypeKey];
    if (userActivity) {
        return YES;
    }
}

return NO;

#4


5  

If the app was closed, application: continueUserActivity: is not called. Instead, you get all the information launchOptions dictionnary in application: didFinishLaunchingWithOptions:

如果应用程序已关闭,则不会调用application:continueUserActivity:。相反,您将在应用程序中获取所有信息launchOptions dictionnary:didFinishLaunchingWithOptions:

// In application: didFinishLaunchingWithOptions:
NSDictionary *activityDic = [launchOptions objectForKey:UIApplicationLaunchOptionsUserActivityDictionaryKey];  
if (activityDic) {
    // Continue activity here
}

#5


3  

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