Receiving nil in callback of dynamicLinks.handleUniversalLink(url)

Viewed 2018

I am working on receiving firebase dynamicLinks. In restorationHandler method of App delegate, I have called DynamicLinks.dynamicLinks().handleUniversalLink(url) method of Firebase DynamicLinks to get the the actual link from shortlink. But in callback of this method I am receiving nil in dynamiclink. So, I am unable to get the url from dynamic link received in callback. Can anyone please help me to figure this out why it is returning nil.

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
    }
2 Answers

I have found a way Expand a short URL in Swift to get actual url or query parameters coming in short url which were nil in my case because I received dynamiclink in completion handler

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

nil. These lines of code will do the same in order to get actual url or query parameters from short 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()

I had the same problem and for me the reason was that I forgot to add my custom url to my info.plist as described here:

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

Here's a preview of the snippet you should add to your info.plist replacing string with your full dynamic link url.

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

I am not using react native, but the solution worked for a regular iOS implementation as well.

Related