Firebase DeepLink 在 iOS14 中的 Killed/Terminated 应用程序状态下不起作用

Firebase DeepLink does not work on Killed/Terminated App state in iOS14

我已将 Firebase DeepLink/DynamicLinks/UniversalLink 集成到我们的应用程序中。当 app is runningbackground/foreground state 时,Deep Links 没有问题。但是,当应用处于 not runningkilled/terminated 状态时,应用将启动,但不会调用或初始化触发方法。好像它只触发 URL Scheme。因此,结果,我无法获得有关被点击的深层链接的数据,这是个大问题。

didFinishLaunchingWithOptions:launchOptions 始终为 nil,因此 launchOptions[.url] 为 nil

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool

并且open url:options:不触发

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

当然这个continue userActivity:restorationHandler:不会触发

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool

所以,我只能在没有捕获数据的情况下启动应用程序。任何解决方法或建议表示赞赏。谢谢!

我花了一些时间来解决这个问题。在尝试了一系列解决方法之后,有效的解决方案是使用 SceneDelegate.

迁移到新的 Apple's App Structure

要从终止状态处理 DeepLink,您需要配置此功能:

var window: UIWindow?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    if let userActivity = connectionOptions.userActivities.first, let url = userActivity.webpageURL {            
        DynamicLinks.dynamicLinks().handleUniversalLink(url) { dynamicLink, error in
            guard let dynamicLink = dynamicLink, let url = dynamicLink.url else { return }
            parseDynamicLink(fromURL: url)
        }
    }
    
    guard let windowScene = (scene as? UIWindowScene) else { return }
    
    // Initialize UIWindow
    window = UIWindow(windowScene: windowScene)
    window?.rootViewController = YOUR_ROOT_VIEW_CONTROLLER
    window?.makeKeyAndVisible()
}

处理与 UIApplication 等效的场景委托的其他部分:

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    guard let url = userActivity.webpageURL else { return }
    DynamicLinks.dynamicLinks().handleUniversalLink(url) { dynamicLink, error in
        guard let dynamicLink = dynamicLink, let url = dynamicLink.url else { return }
        parseDynamicLink(fromURL: url)
        
        // launch dynamic link code here...?
    }    
}

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let urlContext = URLContexts.first else { return }

    if let dynamicLink = DynamicLinks.dynamicLinks().dynamicLink(fromCustomSchemeURL: urlContext.url) {
        guard let url = dynamicLink.url else { return }
        parseDynamicLink(fromURL: url)
    }    
}

如果您打算进行迁移,请确保正确配置您的 Info.plistApplication Scene Manifest,然后在此处设置您的场景配置:

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

您的 Application Scene Manifest 应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>UIApplicationSupportsMultipleScenes</key>
        <false/>
        <key>UISceneConfigurations</key>
        <dict>
            <key>UIWindowSceneSessionRoleApplication</key>
            <array>
                <dict>
                    <key>UISceneDelegateClassName</key>
                    <string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
                    <key>UISceneStoryboardFile</key>
                    <string>Main</string>
                    <key>UISceneConfigurationName</key>
                    <string>Default Configuration</string>
                </dict>
            </array>
        </dict>
    </dict>
</plist>