1

我正在尝试使用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()
4

0 回答 0