首页 > 解决方案 > 深层链接 URL 方案不调用任何函数(swift)

问题描述

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

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

苹果文档说您应该在 AppDelegate 中覆盖应用程序(打开 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
} 

swift 5 模拟器:iOS 13

标签: iosswiftdeep-linkingurl-scheme

解决方案


经过几天与方案网址的斗争,我终于找到了答案。我的模拟器的 iOS 版本是 13 。在 iOS 13 UIApplication 中,打开的 url 没有被调用(至少在我的情况下可能)

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

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

    }
}

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

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


推荐阅读