当应用程序关闭时,iOS 深层链接回调不起作用

问题描述

我在我的应用程序中使用深层链接,并在我的 SceneDelegate 中使用此代码转至视图控制器。

func scene(_ scene: UIScene,openURLContexts URLContexts: Set<UIOpenURLContext>) {
  for context in URLContexts {
    print("url: \(context.url.absoluteURL)")
    print("scheme: \(context.url.scheme ?? "")")
    print("host: \(context.url.host ?? "")")
    print("path: \(context.url.path)")
    print("query: \(context.url.query ?? "")")
    print("components: \(context.url.pathComponents)")
  }
    window?.rootViewController?.performSegue(withIdentifier: "splashToCreateNewPassword",sender: nil)
}

当应用程序已经在后台打开时,它工作得很好,但是当用户关闭应用程序时,它就不起作用了。它只是打开应用程序的第一个屏幕。

解决方法

您可以从这个委托 func scene(_ scene: UIScene,willConnectTo session: UISceneSession,options connectionOptions: UIScene.ConnectionOptions)

获取 URL
func scene(_ scene: UIScene,options connectionOptions: UIScene.ConnectionOptions) {
        if let userActivity = connectionOptions.userActivities.first {
            if let incomingURL = userActivity.webpageURL {
                window?.rootViewController?.performSegue(withIdentifier: "splashToCreateNewPassword",sender: nil)
            }
        }
    }