5

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
    }
Azhar Tahir
  • 185
  • 1
  • 11
  • Check this link below to find the solution. [nil in callback of dynamicLinks.handleUniversalLink](https://stackoverflow.com/questions/59150908/dynamic-link-object-has-no-url/62037224#62037224) – Purnendu roy May 27 '20 at 07:16
  • Click below to find the solution: [Dynamic link object has no url](https://stackoverflow.com/questions/59150908/dynamic-link-object-has-no-url/62037224#62037224) – Purnendu roy May 27 '20 at 07:18

2 Answers2

2

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.

  • Great answer. As soon as custom url is added the link has to be placed in info.plist, because in most cases it has to be different from the domain itself (for example link.mydomain.com vs mydomain.com). Google's doc about this: https://firebase.google.com/docs/dynamic-links/custom-domains#using_your_web_domain_for – Vitalii Sep 07 '21 at 15:29
1

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()
Azhar Tahir
  • 185
  • 1
  • 11