Ios 在应用程序I关闭时继续用户活动
我正在尝试使用Ios 在应用程序I关闭时继续用户活动,ios,swift,appdelegate,nsuseractivity,Ios,Swift,Appdelegate,Nsuseractivity,我正在尝试使用AppDelegate类在我的应用程序中实现通用链接。当应用程序已经在后台打开时,它可以正常工作,但当应用程序不在后台时,它的行为会很奇怪,特别是当应用程序不在后台时,它只能工作1次。例如:当我第一次启动我的应用程序时,多任务然后滑出应用程序,我可以单击一个链接,它将导航到应用程序中的正确位置。如果我再次尝试,它将无法工作,但如果我重新启动应用程序,它将再次工作。请记住,如果应用程序在后台,则每次都会使用通用链接 以下是我的AppDelegate的外观: func applicat
AppDelegate
类在我的应用程序中实现通用链接。当应用程序已经在后台打开时,它可以正常工作,但当应用程序不在后台时,它的行为会很奇怪,特别是当应用程序不在后台时,它只能工作1次。例如:当我第一次启动我的应用程序时,多任务然后滑出应用程序,我可以单击一个链接,它将导航到应用程序中的正确位置。如果我再次尝试,它将无法工作,但如果我重新启动应用程序,它将再次工作。请记住,如果应用程序在后台,则每次都会使用通用链接
以下是我的AppDelegate
的外观:
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
if let url = userActivity.webpageURL {
self.continueWithLink(url: url)
}
return true
}
在didFinsihLaunchingWithOptions
函数中:
self.window = UIWindow(frame: UIScreen.main.bounds)
if let url = launchOptions?[UIApplication.LaunchOptionsKey.url] as? URL {
self.continueWithLink(url: url)
} else {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "MainTabBarViewController")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
continueWithLink
函数将破译链接要导航到的位置,然后调用一个函数来设置所需的viewController,如下所示:
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
if let _ = appDelegate.window?.rootViewController as? BannedViewController { return }
let storyboard = UIStoryboard(name: "Profile", bundle: nil)
let profileVC = storyboard.instantiateViewController(withIdentifier: "ProfileView") as! ProfileViewController
profileVC.shouldNavigateToHome = true
profileVC.shouldNavigateToHomeAction = {
self.loadMainStoryboard()
}
let navigationVC = UINavigationController(rootViewController: profileVC)
appDelegate.window?.rootViewController = navigationVC
appDelegate.window?.makeKeyAndVisible()
就像昨天的问题:你真的打算返回这里吗:
如果让=appDelegate.window?.rootViewController作为?banndviewcontroller{return}
就像昨天一样:从ui的状态中得出真相是一件非常危险的事情。