xcode continueUserActivity 未从搜索关闭的应用程序调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32678078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
continueUserActivity not called from search closed app
提问by Niko Zarzani
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)?.
当我的应用程序关闭时,您是否有任何建议使其工作?有没有办法调试正在发生的事情(因为我需要运行应用程序来连接调试器,我不知道如何模拟从搜索结果中打开的应用程序)?。
采纳答案by Niko Zarzani
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];
}
}
回答by Ivan Oliver
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
伊万
回答by hopye2
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)
回答by adam eliezerov
In the new Swift 5 there is a new new file called SceneDelegate.swift
.
Use the method scene(_ scene: UIScene, continue userActivity: NSUserActivity)
在新的 Swift 5 中有一个名为SceneDelegate.swift
. 使用方法scene(_ scene: UIScene, continue userActivity: NSUserActivity)
回答by cbartel
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;
回答by Baptiste Truchot
If the app was closed, application: continueUserActivity: is not called. Instead, you get all the information launchOptions dictionnary in application: didFinishLaunchingWithOptions:
如果应用程序已关闭,则不会调用 application: continueUserActivity: 。相反,您会在应用程序中获得所有信息 launchOptions 字典:didFinishLaunchingWithOptions:
// In application: didFinishLaunchingWithOptions:
NSDictionary *activityDic = [launchOptions objectForKey:UIApplicationLaunchOptionsUserActivityDictionaryKey];
if (activityDic) {
// Continue activity here
}
回答by Arnab
continue userActivity
is changed in the latest swift version. Changing the func worked for me.
continue userActivity
在最新的 swift 版本中已更改。更改 func 对我有用。
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool
to
到
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool.
回答by Vitalii
Had similar issue while opening Firebase's dynamic links. In my case the problem was that the call has changed and here's what I had in a legacy code, which was never called any more:
打开 Firebase 的动态链接时遇到了类似的问题。就我而言,问题是调用发生了变化,这是我在遗留代码中的内容,它再也没有被调用过:
func application(_ application: UIApplication,
continue userActivity: NSUserActivity,
restorationHandler: @escaping ([Any]?) -> Void) -> Bool
This is the call which worked in my case:
这是在我的情况下工作的电话:
func application(_ application: UIApplication,
continue userActivity: NSUserActivity,
restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool
Pay attention to how exactly the call is written. Don't be deceived by Xcode's warning suggesting to add private
- it won't help.
注意调用的准确写法。不要被 Xcode 建议添加的警告所欺骗private
- 它无济于事。