4
votes

Unable to retrieve the dynamic link from short Url created with Firebase Dynamic links when opened via WhatsApp.

When opened via Skype it shows preview screen but when click on URL using WhatsApp it directly opens the app and not opens the preview screen, after which I am not able to retrieve dynamic link. I am using the following code to create and Shorten the Dynamic link:

guard let link = URL(string: "https://example.com/abc/\(id)") else { return }
        let dynamicLinksDomainURIPrefix = "https://myexample.online"
        let linkBuilder = DynamicLinkComponents(link: link, domainURIPrefix: dynamicLinksDomainURIPrefix)

        linkBuilder?.iOSParameters = DynamicLinkIOSParameters(bundleID: "bundleID")
        linkBuilder?.iOSParameters?.appStoreID = "appStoreID"
        linkBuilder?.androidParameters = DynamicLinkAndroidParameters(packageName: "packageName")
        linkBuilder?.options = DynamicLinkComponentsOptions()
        linkBuilder?.options?.pathLength = .short

        guard let longDynamicLink = linkBuilder?.url else {
            Alert.HideProgressHud(Onview: self.view)
            return
        }
        DispatchQueue.main.async {

            DynamicLinkComponents.shortenURL(longDynamicLink, options: linkBuilder?.options) { url, warnings, error in
                let url = url
                self.openactivityPickerForSharing(capturedImage: capturedImage, url: url)
            }
        }

in AppDelegate I am using the following function o receive this:


        if let dynamicLink = DynamicLinks.dynamicLinks().dynamicLink(fromCustomSchemeURL: url) {
            // Handle the deep link. For example, show the deep-linked content or
            // apply a promotional offer to the user's account.
            // [START_EXCLUDE]
            // In this sample, we just open an alert.
            handleDynamicLink(dynamicLink)
            // [END_EXCLUDE]
            return true
        }
        return application(app, open: url,sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,annotation: "")
    }

    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        if let dynamicLink = DynamicLinks.dynamicLinks().dynamicLink(fromCustomSchemeURL: url) {
            // Handle the deep link. For example, show the deep-linked content or
            // apply a promotional offer to the user's account.
            // [START_EXCLUDE]
            // In this sample, we just open an alert.
            handleDynamicLink(dynamicLink)
            // [END_EXCLUDE]
            return true
        }
        // [START_EXCLUDE silent]
        // Show the deep link that the app was called with.
        //showDeepLinkAlertView(withMessage: "openURL:\n\(url)")
        // [END_EXCLUDE]
        return false
    }
    // [END openurl]
// [START continueuseractivity]
    func application(_ application: UIApplication, continue userActivity: NSUserActivity,
                     restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
        let handled = DynamicLinks.dynamicLinks().handleUniversalLink(userActivity.webpageURL!) { (dynamiclink, error) in
            // [START_EXCLUDE]
           //self.showDeepLinkAlertView(withMessage: "\(dynamiclink)")
            // [END_EXCLUDE]
        }
//handled is always returned always
        // [START_EXCLUDE silent]
        if !handled {
           //Handled the URL received in Case of Skype.
        }
        // [END_EXCLUDE]
        return handled
    }

 func handleDynamicLink(_ dynamicLink: DynamicLink) {
        let matchConfidence: String
        if dynamicLink.matchType == .weak {
            matchConfidence = "Weak"
        } else {
            matchConfidence = "Strong"
        }
        let message = "App URL: \(dynamicLink.url?.absoluteString ?? "")\n" +
        "Match Confidence: \(matchConfidence)\nMinimum App Version: \(dynamicLink.minimumAppVersion ?? "")"
    }

After this when I click on shortened URL shared on WhatsApp, I am receiving the URL which is the shortened url created by Firbase Dynamic Links and not the actual one

2
same issue here.. any luck?Faisal

2 Answers

1
votes

I had a similar issue and I simply forgot to add FirebaseDynamicLinksCustomDomains array to my Info.plist that's why firebase didn't fetch the full url from the short one.

Relevant documentation:
https://firebase.google.com/docs/dynamic-links/custom-domains

0
votes

In the examples, Firebase reads the URL from useractivity.webpageURL. If you would read the url from dynamicLink.url in the closure, you will see the original Dynamic Link.

func application(_ application: UIApplication, continue userActivity: NSUserActivity,
                 restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    let handled = DynamicLinks.dynamicLinks().handleUniversalLink(userActivity.webpageURL!) { (dynamiclink, error) in

      print("This is the full length url: \(dynamiclink.url)")
    }
}