如何解决在我关闭应用程序时继续 userActivity
我正在尝试使用 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()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。