从 link 启动应用程序时,`Branch.initSession` 不会调用 `andRegisterDeepLinkHandler` 回调

`Branch.initSession` does not call `andRegisterDeepLinkHandler` callback when app is launched from the link

我们正在使用 Branch 在我们的 iOS 应用程序中实现深度 linking。现在,如果我 运行 应用程序,然后尝试通过分支 link 打开它, branch.initSession 被调用,我可以访问深层 link 数据。但是,如果我尝试在应用程序未启动时直接打开分支 link,则 branch.initSessionandRegisterDeepLinkHandler 回调不会被执行 - 这基本上使 deep [=22 的意义无效=]ing.

我们的 AppDelegate 代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let branch: Branch = Branch.getInstance()
    branch.initSession(launchOptions: launchOptions, andRegisterDeepLinkHandler: {params, error in

        DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {

            let alert = UIAlertController(title: "Branch", message: "\(params as? [String: AnyObject])", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
            self.window?.rootViewController?.present(alert, animated: false, completion: nil)
        })

        if error == nil {
            // params are the deep linked params associated with the link that the user clicked -> was re-directed to this app
            // params will be empty if no data found
            // TODO: ... insert custom logic here ...
            print("params: %@", params as? [String: AnyObject] ?? {})
        }
    })
    ...
    // facebook SDK login integration
    return SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
}

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    // pass the url to the handle deep link call
    let branchHandled = Branch.getInstance().application(application,
                                                         open: url,
                                                         sourceApplication: sourceApplication,
                                                         annotation: annotation)
    return branchHandled
}

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    // pass the url to the handle deep link call
    let branchHandled = Branch.getInstance().application(app,
                                                         open: url,
                                                         options: options)
    return branchHandled
}

我们已经能够通过以下方式解决此问题。问题是 AppDelegateapplication(_:,didFinishLaunchingWithOptions:) 的 return 值。我们一直在将 return 值的生成委托给 facebook 的 SDKApplicationDelegate.shared。似乎 branch.initSession 需要 application(_:,didFinishLaunchingWithOptions:) 到 return true,而 SDKApplicationDelegate.shared 在问题中提到的那些情况下 returned false.

这已记录在案,但很难在文档中找到它,特别是如果您不知道问题是由同时使用分支和 facebook SDK 引起的。

因此,修复是更新 application(_:,didFinishLaunchingWithOptions:) 实现:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let branch: Branch = Branch.getInstance()
    branch.initSession(launchOptions: launchOptions, andRegisterDeepLinkHandler: {params, error in
        if error == nil {
        // params are the deep linked params associated with the link that the user clicked -> was re-directed to this app
        // params will be empty if no data found
        // TODO: ... insert custom logic here ...
        print("params: %@", params as? [String: AnyObject] ?? {})
        }
        })
    ...

    // facebook SDK login integration
    SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)

    // always return true!
    return true
}

这一切都是我根据 when integration Facebook. I have added an 的回答发起的,并在那里也进行了修复,所以其他人也有可能不会上当。

希望这会节省一些时间。