【问题标题】:Receiving nil in callback of dynamicLinks.handleUniversalLink(url)在 dynamicLinks.handleUniversalLink(url) 的回调中接收 nil
【发布时间】:2020-03-08 13:19:13
【问题描述】:

我正在努力接收 Firebase 动态链接。在 App 委托的 restoreHandler 方法中,我调用了 Firebase DynamicLinks 的 DynamicLinks.dynamicLinks().handleUniversalLink(url) 方法来从短链接获取实际链接。 但是在此方法的回调中,我在动态链接中收到 nil。因此,我无法从回调中收到的动态链接获取 url。谁能帮我弄清楚为什么它返回零。

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

        if let incomingURL = userActivity.webpageURL {
// it prints the passed url and working fine
            debugPrint("Imcoing Url is: \(incomingURL.absoluteString)")

            let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in

                guard error == nil else {
                    debugPrint("Error Found: \(error!.localizedDescription)")
                    return
                }
//
//
                if let dynamiclink = dynamicLink, let _ = dynamiclink.url {
// but below function call is not called beacause dynamicLink coming in
// callback is nil
                    self.handleIncomingDynamicLink(dynamiclink)
                }
            }
            if linkHandled {
                return true
            } else {
                // do other stuff to incoming URL?
                return false
            }
        }

        return false
    }

【问题讨论】:

标签: ios swift xcode firebase deeplink


【解决方案1】:

我找到了一种方法 Expand a short URL in Swift 来获取短 url 中的实际 url 或查询参数,在我的情况下是 nil,因为我在完成处理程序中收到了 dynamiclink

DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in 
    //Both dynamicLink and error were nil here in my case 
}

无。这些代码行将执行相同的操作,以便从短 URL 获取实际 url 或查询参数。

URLSession.shared.dataTask(with: incomingURL) { (data, response, error) in

            if let actualURL = response?.url {

                // you will get actual URL from short link here

                // e.g short url was  
                // https://sampleuniversallink.page.link/hcabcx2fdkfF5U

                // e.g actual url will be  
                // https://www.example.com/somePage?quertItemkey=quertItemvalue...
            }
        }.resume()

【讨论】:

  • 只返回桌面链接,不返回针对移动设备的链接。
【解决方案2】:

我遇到了同样的问题,对我来说,原因是我忘记将自定义 url 添加到我的 info.plist 中,如下所述:

https://rnfirebase.io/dynamic-links/usage#dynamic-links-with-custom-domains

这是你应该添加到你的 info.plist 的 sn-p 的预览,用你的完整动态链接 url 替换字符串。

<key>FirebaseDynamicLinksCustomDomains</key>
  <array>
    <string>https://custom.domain.io/bla</string>
    <string>https://custom.domain.io/bla2</string>
  </array>

我没有使用本机反应,但该解决方案也适用于常规 iOS 实现。

【讨论】:

  • 很好的答案。一旦添加了自定义 url,就必须将链接放在 info.plist 中,因为在大多数情况下,它必须与域本身不同(例如 link.mydomain.com 与 mydomain.com)。 Google 的文档:firebase.google.com/docs/dynamic-links/…
猜你喜欢
  • 2022-01-10
  • 1970-01-01
  • 2018-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-26
  • 2023-03-04
相关资源
最近更新 更多