Receive Dynamic Links on iOS

To receive the Firebase Dynamic Links that you created, 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.

Prerequisites

Firebase Dynamic Links requires iOS 8 or newer. You can target iOS 7 in your app, but all Firebase Dynamic Links SDK calls will be no-ops if the app isn't running on iOS 8 or newer.

Set up Firebase and the Dynamic Links SDK

  1. Add Firebase to your iOS project. Include the following pod in your Podfile:
    pod 'Firebase/DynamicLinks'
  2. Run pod install and open the created .xcworkspace file.
  3. In the Firebase console, open the Dynamic Links section.
    1. Accept the terms of service if you are prompted to do so.
    2. Take note of your project's Dynamic Links domain, which is displayed at the top of the Dynamic Links page. You need your project's Dynamic Links domain to programmatically create Dynamic Links. A Dynamic Links domain look like app_code.app.goo.gl.

  4. Ensure 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.

    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
    If your app is connected, the apple-app-site-association file contains a reference to your app's App Store ID and bundle ID. For example:
    {"applinks":{"apps":[],"details":[{"appID":"1234567890.com.example.ios","paths":["/*"]}]}}
    If the details field is empty, double-check that you specified your Team ID.

Open Dynamic Links in your app

  1. In the Info tab of your app's Xcode project, create a new URL type to be used for Dynamic Links. Set the Identifier field to a unique value and the URL scheme field to either your bundle identifier or a unique value. If you set the URL scheme to a value other than your bundle identifier, you must specify the bundle identifier when you create your Dynamic Links.
  2. In the Capabilities tab of your app's Xcode project, enable Associated Domains and add the following to the Associated Domains list:
    applinks:app_code.app.goo.gl
  3. Import the Firebase module:

    Objective-C

    @import Firebase;

    Swift

    import Firebase
  4. Then, in the didFinishLaunchingWithOptions: method, configure a FIRApp shared instance:

    Objective-C

    - (BOOL)application:(UIApplication *)application
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      // Set deepLinkURLScheme to the custom URL scheme you defined in your
      // Xcode project.
      [FIROptions defaultOptions].deepLinkURLScheme = CUSTOM_URL_SCHEME;
      [FIRApp configure];
    
      return YES;
    }

    Swift

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
      // Set deepLinkURLScheme to the custom URL scheme you defined in your
      // Xcode project.
      FIROptions.default().deepLinkURLScheme = self.customURLScheme
      FIRApp.configure()
    
      return true
    }
  5. Next, in the application:openURL:sourceApplication:annotation: (iOS 8 and older) and application:openURL:options: (iOS 9) methods, handle links received through your app's custom URL scheme. These methods are called when your app receives a link on iOS 8 and older, and when your app is opened for the first time after installation on any version of iOS.

    Objective-C

    - (BOOL)application:(UIApplication *)app
                openURL:(NSURL *)url
                options:(NSDictionary<NSString *, id> *)options {
      return [self application:app openURL:url sourceApplication:nil annotation:@{}];
    }
    
    - (BOOL)application:(UIApplication *)application
                openURL:(NSURL *)url
      sourceApplication:(NSString *)sourceApplication
             annotation:(id)annotation {
      FIRDynamicLink *dynamicLink =
      [[FIRDynamicLinks dynamicLinks] dynamicLinkFromCustomSchemeURL:url];
    
      if (dynamicLink) {
        // Handle the deep link. For example, show the deep-linked content or
        // apply a promotional offer to the user's account.
        // ...
        return YES;
      }
    
      return NO;
    }

    Swift

    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. For example, show the deep-linked content or
        // apply a promotional offer to the user's account.
        // ...
        return true
      }
    
      return false
    }
  6. Finally, in the application:continueUserActivity:restorationHandler: method, handle links received as Universal Links on iOS 9 and newer:

    Objective-C

    - (BOOL)application:(UIApplication *)application
    continueUserActivity:(NSUserActivity *)userActivity
     restorationHandler:(void (^)(NSArray *))restorationHandler {
    
      BOOL handled = [[FIRDynamicLinks dynamicLinks]
                 handleUniversalLink:userActivity.webpageURL
                 completion:^(FIRDynamicLink * _Nullable dynamicLink,
                              NSError * _Nullable error) {
                   // ...
                 }];
    
    
      return handled;
    }

    Swift

    @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
        // ...
      }
    
    
      return handled
    }

Send feedback about...

Need help? Visit our support page.