Search code examples
iosswiftxcodefirebasedeeplink

Receiving nil in callback of dynamicLinks.handleUniversalLink(url)


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
    }

Solution

  • 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()