Create Dynamic Links on iOS
You can create short or long Dynamic Links with the Firebase Dynamic Links Builder API. This API accepts either a long Dynamic Link or an object containing Dynamic Link parameters, and returns URLs like the following examples:
Prerequisites
Firebase Dynamic Links requires iOS 8 or newer. You can target iOS 7 in your app, but Firebase Dynamic Links SDK calls only function on apps running iOS 8 or newer.
Set up Firebase and the Dynamic Links SDK
- Add Firebase to your iOS project. Include the following pod in your
Podfile
: - Run
pod install
and open the created.xcworkspace
file. - In the Firebase console, open the Dynamic Links section.
- If you have not already accepted the terms of service and set a URI prefix for your Dynamic Links, do so when prompted.If you already have a Dynamic Links URI prefix, take a note of it. You need to provide it when you programmatically create Dynamic Links.
- Recommended: Specify the URL patterns allowed in your deep links and fallback links. By doing so, you prevent unauthorized parties from creating Dynamic Links that redirect from your domain to sites you don't control. See Whitelist URL patterns.
- Ensure that your app's App Store ID and your App ID prefix 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.Confirm that your Firebase project is properly configured to use Dynamic Links in your iOS app by opening the
apple-app-site-association
file that is hosted on your Dynamic Links domain. For example:If your app is connected, theapple-app-site-association
file contains a reference to your app's App Store ID and bundle ID. For example:If thedetails
property is empty, double-check that you specified your App ID prefix. Note that your App ID prefix may not be the same as your Team ID.
Add Firebase to your app
- Import the Firebase module in your
UIApplicationDelegate
: - Configure a
FirebaseApp
shared instance, typically in your app'sapplication:didFinishLaunchingWithOptions:
method:
Use the Firebase console
If you want to generate a single Dynamic Link, either for testing purposes, or for your marketing team to easily create a link that can be used in something like a social media post, the simplest way would be to visit the Firebase console and create one manually following the step-by-step form.
Use the iOS Builder API
You can use the iOS Builder API to build Dynamic Links from parameters, or to shorten a long Dynamic Link.
Create a Dynamic Link from parameters
To create a Dynamic Link, create a new
DynamicLinkComponents
object and specify the Dynamic Link parameters by setting the object's corresponding properties. Then, get the long link from the object's url
property or get the short link by calling shorten()
.
The following minimal example creates a long Dynamic Link to
https://www.example.com/my-page
that opens with your iOS app on iOS and the app com.example.android
on Android:
To create a short Dynamic Link, build a
DynamicLinkComponents
the same way, and then call shorten()
.
Building a short link requires a network call, so instead of directly returning the link,
shorten()
accepts a completion handler, which is called when the request completes. For example:
By default, short Dynamic Links are generated with 17-character link suffixes that make it extremely unlikely that someone can guess a valid Dynamic Link. If, for your use case, there's no harm in someone successfully guessing a short link, you might prefer to generate suffixes that are only as long as necessary to be unique, which you can do by setting the
dynamicLinkComponentsOptions
property:Dynamic Link parameters
You can use the Dynamic Link Builder API to create Dynamic Links with any of the supported parameters. See the API reference for details.
The following example creates a Dynamic Link with several common parameters set:
You can set Dynamic Link parameters with the following objects and properties:
DynamicLinkComponents | |
---|---|
link |
The link your app will open. Specify a URL that your app can handle, typically the app's content or payload, which initiates app-specific logic (such as crediting the user with a coupon or displaying a welcome screen). This link must be a well-formatted URL, be properly URL-encoded, use either HTTP or HTTPS, and cannot be another Dynamic Link.
|
domainURIPrefix | Your Dynamic Link URL prefix, which you can find in the Firebase console. A Dynamic Link domain looks like the following examples: |
DynamicLinkAndroidParameters | |
---|---|
fallbackURL | The link to open when the app isn't installed. Specify this to do something other than install your app from the Play Store when the app isn't installed, such as open the mobile web version of the content, or display a promotional page for your app. |
minimumVersion | The versionCode of the minimum version of your app that can open the link. If the installed app is an older version, the user is taken to the Play Store to upgrade the app. |
DynamicLinkIOSParameters | |
---|---|
appStoreID | Your app's App Store ID, used to send users to the App Store when the app isn't installed |
fallbackURL | The link to open when the app isn't installed. Specify this to do something other than install your app from the App Store when the app isn't installed, such as open the mobile web version of the content, or display a promotional page for your app. |
customScheme | Your app's custom URL scheme, if defined to be something other than your app's bundle ID |
iPadFallbackURL | The link to open on iPads when the app isn't installed. Specify this to do something other than install your app from the App Store when the app isn't installed, such as open the web version of the content, or display a promotional page for your app. |
iPadBundleID | The bundle ID of the iOS app to use on iPads to open the link. The app must be connected to your project from the Overview page of the Firebase console. |
minimumAppVersion | The version number of the minimum version of your app that can open the link. This flag is passed to your app when it is opened, and your app must decide what to do with it. |
DynamicLinkNavigationInfoParameters | |
---|---|
forcedRedirectEnabled | If set to '1', skip the app preview page when the Dynamic Link is opened, and instead redirect to the app or store. The app preview page (enabled by default) can more reliably send users to the most appropriate destination when they open Dynamic Links in apps; however, if you expect a Dynamic Link to be opened only in apps that can open Dynamic Links reliably without this page, you can disable it with this parameter. Note: the app preview page is only shown on iOS currently, but may eventually be shown on Android. This parameter will affect the behavior of the Dynamic Link on both platforms. |
DynamicLinkSocialMetaTagParameters | |
---|---|
title | The title to use when the Dynamic Link is shared in a social post. |
descriptionText | The description to use when the Dynamic Link is shared in a social post. |
imageURL | The URL to an image related to this link. The image should be at least 300x200 px, and less than 300 KB. |
DynamicLinkGoogleAnalyticsParameters | |
---|---|
source medium campaign term content | Google Play analytics parameters. These parameters (utm_source , utm_medium , utm_campaign , utm_term , utm_content ) are passed on to the Play Store as well as appended to the link payload. |
DynamicLinkItunesConnectAnalyticsParameters | |
---|---|
providerToken affiliateToken campaignToken | iTunes Connect analytics parameters. These parameters (pt , at , ct ) are passed to the App Store. |
Shorten a long Dynamic Link
To shorten a long Dynamic Link, pass the long Dynamic Link to
shortenURL(url:options:)
along with aDynamicLinkComponentsOptions
object if you want to generate a link with a short suffix:Specifying a custom URL scheme for Dynamic Links
By default, Dynamic Links uses your app's bundle identifier as the URL scheme needed to open up your application. We recommend staying with this default value to keep your implementation simple.
However, developers who are already using a custom URL scheme for other purposes may wish to use this same custom URL scheme for their Dynamic Links as well. If you are in this situation, you can specify a different URL scheme for your Firebase Dynamic Links by following these steps:
- When setting up your app, make sure you specify the default URL scheme to be used by your application before configuring your
FirebaseApp
shared instance: - Whenever you create any Dynamic Link, you will need to specify the custom URL scheme that your app uses. You can do this through the Firebase console, setting the
customScheme
in the Builder API, specifying theius
parameter in your URL, or sending theiosCustomScheme
parameter to the REST API
Comments
Post a Comment