Firebase Deeplink 未在 Swift 中调用 AppDelegate 的 application:continueUserActivity:restorationHandler 函数 3

Firebase Deeplink not calling application:continueUserActivity:restorationHandler function of AppDelegate in Swift 3

我正在使用 firebase Deeplink URL 打开我的应用程序的特定部分。当应用程序 运行ning 在后台时它运行良好但是当我杀死应用程序并从外部单击 deeplink url 时我不知道如何处理这种情况,我的意思是在哪里我应该写我的条件来获取 url.

的参数

当应用在后台时调用此应用委托方法

   @available(iOS 8.0, *)
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
    guard let dynamicLinks = DynamicLinks.dynamicLinks() else {
        return false
    }
    let handled = dynamicLinks.handleUniversalLink(userActivity.webpageURL!) { (dynamiclink, error) in
        if let dynamicLink = dynamiclink, let _ = dynamicLink.url
        {
            var path = dynamiclink?.url?.path
            path?.remove(at: (path?.startIndex)!)
            let delimiter = "/"
            var fullNameArr = path?.components(separatedBy: delimiter)
            let type: String = fullNameArr![0]
            let Id: String? = (fullNameArr?.count)! > 1 ? fullNameArr?[1] : nil
            if(type == "games")
            {
                self.callGameDetailView(gameId: Id! , gameType: "created", notificationId: "NIL" )

            }else{
                var paths = dynamicLink.url?.path
                paths?.remove(at: (path?.startIndex)!)
                self.setRewardViewController(paths!)

            }
        } else {
            // Check for errors
        }
    }
    return handled
}

但当我终止应用程序并点击动态 link url:

时,它不会调用打开 url 方法
@available(iOS 9.0, *)
func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any])
    -> Bool {

   return self.application(application, open: url, sourceApplication: nil, annotation: [:])


}


func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {

    let dynamicLink = DynamicLinks.dynamicLinks()?.dynamicLink(fromCustomSchemeURL: url)
    if let dynamicLink = dynamicLink
    {
        var path = dynamicLink.url?.path
        path?.remove(at: (path?.startIndex)!)
        let delimiter = "/"
        var fullNameArr = path?.components(separatedBy: delimiter)
        let type: String = fullNameArr![0]
        let Id: String? = (fullNameArr?.count)! > 1 ? fullNameArr?[1] : nil
        if(type == "games")
        {
            self.callGameDetailView(gameId: Id! , gameType: "created", notificationId: "NIL" )

        }else{
            var paths = dynamicLink.url?.path
            paths?.remove(at: (path?.startIndex)!)
            self.setRewardViewController(paths!)
            return true
        }
    }

    return FBSDKApplicationDelegate.sharedInstance().application(
        application,
        open: url,
        sourceApplication: sourceApplication,
        annotation: annotation)
}

简而言之,我不知道如何处理应用程序第一次 运行 或终止应用程序后 运行 时的动态 link?

您引用的两个 openURL AppDelegate 方法在使用 URI 方案打开应用程序时被调用。 URI 方案功能仅适用于非常特定的场景。在 iOS 9 中,Apple 切换到 Universal Links,它实际上调用了函数 application:continueUserActivity:restorationHandler:

此函数不调用 openURL 方法,因此您所有的通用链接处理都必须在您首先提到的 continueUserActivity 中完成。

为了避免在处理其他边缘情况时遇到很多麻烦,我建议转向另一个第三方提供商,例如 Branch(完全公开,我在那里工作),因为它们捆绑了所有这些处理一个回调并提供更多 Firebase 用户无法使用的功能和归因相关服务。

在application:didFinishLaunchingWithOptions:方法中,您需要注册您的customURLScheme。

FirebaseOptions.defaultOptions()?.deepLinkURLScheme = YOUR_URL_SCHEME

我也有同样的问题,你需要从 launchOptions 获取 NSUserActivity

var launchURL :URL? = nil

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            ....
            // this code should have worked but not working for me with Xcode 9
            //        if let userActivityDictionary = launchOptions?[.userActivityDictionary] as? [UIApplicationLaunchOptionsKey : Any],
            //            let auserActivity = userActivityDictionary[.userActivityType] as? NSUserActivity {
            //            launchURL = auserActivity.webpageURL
            //        }

             if let userActDic = launchOptions?[UIApplicationLaunchOptionsKey.userActivityDictionary] as? [String: Any],
                        let  auserActivity  = userActDic["UIApplicationLaunchOptionsUserActivityKey"] as? NSUserActivity{

                        NSLog("type \(userActDic.self),\(userActDic)") // using NSLog for logging as print did not log to my 'Device and Simulator' logs
                        launchURL = auserActivity.webpageURL
                    }
            ...
            }

func applicationDidBecomeActive(_ application: UIApplication) {
            if let url = self.launchURL {
                self.launchURL = nil

                DispatchQueue.main.asyncAfter(deadline: .now()+2.0, execute: {
                    // wait to initalize notifications

                    if let dynamicLinks = DynamicLinks.dynamicLinks()  {
                        NSLog("handling \(dynamicLinks)")
                        dynamicLinks.handleUniversalLink(url) { (dynamiclink, error) in
                            if let link = dynamiclink?.url {
                                NSLog("proceessing \(dynamicLinks)")
                                let strongMatch = dynamiclink?.matchConfidence == .strong
    // process dynamic link here
                                self.processDynamicLink(withUrl: link, andStrongMatch: strongMatch)
                            }
                        }

                    }

                })
            }

        }

我也整天面对同样的问题。 Firebase SDK 不处理这种边缘情况,也没有在他们的文档中的任何地方提及。非常令人沮丧!

在调查 iOS 13 和更高版本 OS 后找到了解决方案。 Firebase Dynamic link 正在运行并且还能够重定向到特定页面,即使应用 运行 是第一次或 运行 终止应用后也是如此。

SceneDelegate 中使用以下代码代替 AppDelegate

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

if let firstUrlContext = connectionOptions.userActivities.first,let url = firstUrlContext.webpageURL  {
    DynamicLinks.dynamicLinks().handleUniversalLink(url) { (dynamiclink, error) in
        if let dynamiclink = dynamiclink {
            self.handleDynamicLink(dynamiclink)
        }
      }
    }
  }