ios - Firebase Deeplink 不调用应用程序 :continueUserActivity:restorationHandler function of AppDelegate in Swift 3

标签 ios swift firebase deep-linking firebase-dynamic-links

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

app delegate 方法在后台调用

   @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
}

但当我终止应用程序并点击动态链接 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)
}

简而言之,我不知道如何处理应用首次运行时或杀死应用后运行的动态链接?

最佳答案

我也有同样的问题,你需要从 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)
                            }
                        }

                    }

                })
            }

        }

关于ios - Firebase Deeplink 不调用应用程序 :continueUserActivity:restorationHandler function of AppDelegate in Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46481509/

相关文章:

ios - 当加载WKWebview时,它是nil

Swift 2.0 按值传递

android - 如何停止在 android 中收听 firebase 位置

firebase - 在 flutter 中使用 withConverter 获取 Firestore 文档引用字段的数据

android - 如何在 Android 的 RecyclerView 中显示来自 Firebase 节点的所有子节点?

ios - 如何将 UITableView 设置为捕捉到最近的单元格?

ios - nodeBlockForRowAtIndexPath 中的异步调用

ios - 空指针的 Xcode 静态分析

ios - 游戏背景 - PNG 文件的正确分辨率?

swift - 为什么我可以为 NSWindowController 的子类调用一个无参数的 init()?