Firebase Dynamic Links

Shivani Gor
IndiaNIC
Published in
5 min readMay 22, 2017

First we will talk about Deep linking concept in brief. The concept of Deep linking is to open mobile app’s specific content from another app or open mobile app on click of web link. Let’s have an example. First share your Instagram post to Twitter. Now go to Twitter app or open twitter.com in mobile browser. Now see your Instagram post in your tweets. Now click on that post this redirects you to your own post on Instagram app if it installed, otherwise it will open that post in browser.

So using deep linking you can redirect user to your app instead of showing content on web which is more user friendly and convenient. Message passing to other apps is also possible. This can be achieved using custom url schema .

Now you think, why we need Firebase Dynamic Links if we can achieve this with deep linking.

But limitation of Deep linking is that, this will not work for the users who have not installed an application. For example you want to show personalized welcome message to app users or want to show promotional offers or want to redirect him to specific screen in app by clicking deep link. Now what if user has not installed your app?

In this scenario, Firebase Dynamic Links can offer you a right solution. These are deep links that work for your all needs. By creating one single dynamic link, you can redirect user to appropriate platform. Clicking on this link mobile app users are redirected to their native app and web users are redirected to your website for specific content.

You can also customize this behaviour. If user has not installed your app, you can take him to app store/play store to download app or take him to show content on mobile browser. When user clicks a dynamic link and installs an application, information is still available on first time user opens app.

Firebase Dynamic Links requires iOS 8 or newer.

How to create Firebase Dynamic Links?

You can create Firebase Dynamic Links in three ways.

  1. Using Firebase Console
  2. Using a REST API
  3. By forming a URL by adding Dynamic Link parameters to a domain specific to your app.

Create Dynamic Links Using Firebase Console

This is useful for creating one link sharing on social media.

  1. Click on Dynamic Links section in your Firebase console project.
  2. Note down your Firebase Dynamic Link domain and click on NEW DYNAMIC LINK.

3) Fill the required information and finally click CREATE DYNAMIC LINK.

Receive Dynamic Links on iOS

To receive the Firebase Dynamic Links that you created earlier, you must include the Dynamic Links SDK in your app and call the handleUniversalLink: and dynamicLinkFromCustomSchemeURL: methods when your app loads to get the data passed in the Dynamic Link.

Set up Firebase Dynamic Links SDK

  1. First of all set up Firebase to your iOS project. If you are new to Firebase, you can check setup Firebase here.
  2. Add pod ‘Firebase/DynamicLinks’ in your pod file.
  3. Run pod install and open workspace file.
  4. Check that your app’s App Store ID and your Apple Developer Team ID is specified in your app’s settings. To view and edit your app’s settings, go to your Firebase project’s Settings page and select your iOS app.
  5. You can confirm that your Firebase project is properly configured to use Dynamic Links in your iOS app by opening the following URL: https://app_code.app.goo.gl/apple-app-site-association

Here https://app_code.app.goo.gl is your Firebase Dynamic Link Domain which you note down earlier.

If you have setup all properly, you will get the response like this:

{“applinks”:{“apps”:[],”details”:[{“appID”:”1234567890.com.example.ios”,”paths”:[“/*”]}]}}

Now open your Xcode project and go to Build Settings Info tab.In URL Types add new URL type to use for Dynamic Links. Set the Identifier field to a unique value and set the URL Schemes to your app’s bundle identifier or other unique value.

It’s best practice to set the URL scheme to your app bundle identifier otherwise you have to specify your bundle identifier while creating your Dynamic Links.

Next enable Associated Domains under Capabilities tab. Add the following value to the Associated Domains list:

Applinks:app_code.app.goo.gl

Import Firebase in AppDelegate class.

In the didFinishLaunchingWithOptions: method configure FIRApp and set the deepLinkURLScheme to the URL Schemes you set in URL Types.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:  [UIApplicationLaunchOptionsKey: Any]?) -> Bool {   // Override point for customization after application launch.
// Use Firebase library to configure APIs
FIROptions.default().deepLinkURLScheme = “Your_App_URL_Scheme”
FIRApp.configure()
return true
}

When your app is opened first time by user after installation application:openURL:sourceApplication:annotation: method is called. So you can write your code to show welcome message or show promotional offer in this method.

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {   return application(app, open: url, sourceApplication: nil,     annotation: [:])}func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {   let dynamicLink =   FIRDynamicLinks.dynamicLinks()?.dynamicLink(fromCustomSchemeURL: url)   if let dynamicLink = dynamicLink {   // Handle the deep link here.   // Show promotional offer.   print(“Dynamic link : \(dynamicLink.url)”)   return true  }  return false
}

When your app receives dynamic link application:continueUserActivity:restorationHandler: method is called. Deep link is treated as Universal Links on iOS 9 and newer. You can get the dynamic link url by dynamiclink.url property in callback. Based on url path you can write your redirection code in this method.

@available(iOS 8.0, *)func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {   guard let dynamicLinks = FIRDynamicLinks.dynamicLinks() else {   return false   }   let handled =   dynamicLinks.handleUniversalLink(userActivity.webpageURL!) { (dynamiclink, error) in      // Handle the deep link here.      // Redirect user to specific screen according to requirement.        print(“Dynamic link : \(dynamiclink?.url)”)      let path = dynamiclink?.url?.path      if path == “/home” {         print(“Redirect to home screen”)
}
else
{
print(“Redirect to another screen”)
}

}
return handled}

I hope this post will give you basic idea of creating Firebase Dynamic Link using Firebase Console and setting Firebase Dynamic Links SDK on iOS. For other two options you can check Firebase documentation here.

Happy Coding!!!

--

--