深层链接 URL Scheme 不调用任何函数(swift)

Arv*_*aei 9 url-scheme deep-linking ios swift

即时实现深层链接是iOS。我已经在 Project-Setting->Info->Url type URL Schemes 中配置了 URL Schemes : carwash role:Viewer

当我输入 carwash://something 时,浏览器要求打开应用程序,但在应用程序中没有调用我处理应该发生的操作。

苹果文档说你应该在 AppDelegate 中覆盖 application(open url) 但深层链接不调用它并且应用程序在最后状态打开

application:openURL:options:' 未被调用

这是我的代码并且不起作用

 func application(_ app: UIApplication, open url: URL,
                     options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        fatalError()
    }

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        if let url = launchOptions?[UIApplication.LaunchOptionsKey.url] as? URL {
           /// some
            fatalError()
        }
    GMSServices.provideAPIKey("")

    return true
} 
Run Code Online (Sandbox Code Playgroud)

swift 5 模拟器:iOS 13

Arv*_*aei 10

经过几天与计划网址的斗争,我终于找到了答案。我的模拟器的 iOS 版本是 13 。在 iOS 13 UIApplication 中,open url 不会被调用(至少在我的情况下)

您应该在 iOS 13 中可用的 SceneDelegate 中覆盖以下函数:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    if let url = URLContexts.first?.url{
        print(url)

    }
}
Run Code Online (Sandbox Code Playgroud)

已编辑:如果用户在您的应用未运行时点击链接,您将不会响应它

您必须实现scene(_:willConnectTo:options:)以检测 options: 中的 URLContext 并处理它。

  • 对了一半。如果 URL 在您的应用程序未运行时到达,则该方法将不起作用。 (2认同)
  • 这不是我“建议”什么的问题,这是事实。您必须实现“scene(_:willConnectTo:options:)”来检测“options:”中的 URLContext 并处理它。否则,如果用户在您的应用未运行时点击链接,您将不会响应它。尝试一下,你就会看到。 (2认同)
  • 谢谢水晶球哑光。真的很有帮助的评论。 (2认同)