Deep Link in iOS App by Universal Link
Version: Swift 5 , iOS 14.5 , Xcode 13.0
Overview
I would like to talk about Deep Link
before we start discussing Universal Link.
Universal Link
is a technology name, it can make user tap link and navigate to specific page in specific iOS app.
Currently, there are two main method to make Deep Link
.
- Before iOS 9 , using
URL Scheme
. - Since iOS 9 in 2015, using
Universal Link
.
And another relation technologies, such as Defferred Deep Link
, Firebase Dynamic Link
, I will write another articles to discuss .
Since Universal Link
is newer, so this article will focus on Universal Link
.
Universal Link Flow
Limit
There are some limits to use Universal Link
- You have to owner
Domain
. - Your server have to support
HTTPS.
- You can upload files to your
Web Server
. - Your app deployment version have to greater than or equal to
iOS 9.
- Xcode version greater than or equal to
7
.
Principle
When iOS SDK
install App
, it will check Info.plist
in the App
, if it find .entitlements
, it will look for Associated Domains Value
of applinks
, and it will look for apple-app-site-association
file in Https WebServer
, and download to local, make the domain
and app
link after parse.
By the way, this is why Universal Link
more secure, because no one can upload apple-app-site-association
file to Web Server
, only you.
If you are confused about the principle, don’t worry, I will show you step by step.
Getting Started
Implement Universal Link
is not complex, there are two big steps.
- Create link between
App
andWeb Server
. - Handle
App
page navigate whenApp
be open fromUniversal Link
Step 1.0
Enable Associated Domains in Xcode
Add the Domains
you want to link, for example, if your domain
is 「testdomain.com
」, type in 「applinks:testdomain.com
」here.
After add domain
in Associated Domains
, you will find a file the extension name is 「.entitlements
」 in Xcode file directory
App will look for the
domain
that you created inAssociated Domains
at app first install , and app will look forapple-app-site-association
file inWeb Server
to create link betweenApp
andWeb Server
.
Step 1.1
Create 「apple-app-site-association」
file
In previous step, we configure domain
that we want to link and know App
will look for domain
to create link between App
and Web Server
, so in this step , we will create apple-app-site-association
file.
Create a file named 「apple-app-site-association
」 using TextEdit App, and press 「shift + command + T
」, it will make the file be pure text file.
Follow the format from following image.
Description
- appID:Compose of
TeamID
andBundleID
, its format isTeamID.BundleID
. You can findTeamID
inApple Developer
and findBundleID
inXcode
. - paths:It’s mean the
path
you want to supportUniversal Link
, it can be acollection
, if you wantall path
supportUniversal Link
, you can type 「*」
For example, my TeamID is 「AB1234567C
」, BundleID is 「com.testDomain
」, so my appID will be 「AB1234567C.com.testDomain
」
★
After typing complete, save and delete extension name, upload it to
Web Server
and it can’t have any extension name
Step 1.2
Upload 「apple-app-site-association」 to
Web Server
Send the apple-app-site-association
file that you created in previous step to Backend Engineer, and ask him help us to upload the file to one of the two follow path, the iOS Engineer can’t handle this part.
apple-app-site-association
file path
- At web server root, etc.
https://test.com/apple-app-site-association
- At
.well-known
folder(recommend this directory, Apple change the directory iniOS 9.3
, but it still support root directory), if your root directory do not have.well-known
folder, you can create.well-known
folder(don’t forget dots that beforewell-known
), etc.https://test.com/.well-known/apple-app-site-association
Step 1.3
Verify if「apple-app-site-association」 upload was successful
Verify steps are simply, you can open browser and navigate the path that you upload apple-app-site-association
, so you can type 「https://test.com/apple-app-site-association
」or「https://test.com/.well-known/apple-app-site-association
」 in url bar.
If upload successful, you can see download page or json data on web.
In the previous steps, we have completed the link between the App and the web server. Then, let’s see how to handle the page navigate when the App opened with Universal Link.
Step 2.0
Handling navigation after your app is opened by the universal link
When user tap link, open app and trigger specific function, we can get the data that we want by the specific function, for example , we can know the user taped which link to open app, and navigate the specific pages by link path or query.
There are two ways to handle navigation, if window
is managed by AppDelegate.swift
, handle navigation by AppDelegate
, else if window
managed by SceneDelegate.swift
, handle navigation by SceneDelegate
.
If you want to know what’s different between
AppDelegate
andSceneDelegate
, you can reference this article:「iOS最低支援版本低於iOS 13並同時使用AppDelegate以及SceneDelegate」.
Case in AppDelegate.swift :
If your window
be managed in AppDelegate
, when your app is opened by universal link
, application(_:continue:restorationHandler:)
will be triggered.
The code of above image, it implement simply navigate to specific page by url path or query.
Line 154 ~155: Get url path
parameter.
Line 157: Determine whether have parameter「product
」.
Line 159: Initialize the ViewController that will be navigated.
Line 162: Get the UINavigationController
.
Line 164: Navigate to the productVC
Case in SceneDelegate.swift :
If your window
be managed in SceneDelegate
, when your app is opened by universal link, scene(_:continue:)
will be triggered.
The code of above image, it implement simply navigate to specific page by url path or query.
Line 37 ~ 38: Get url path
parameter.
Line 40: Determine whether have parameter「product
」.
Line 42: Initialize the ViewController that will be navigated.
Line 44: Get the UINavigationController
.
Line 46: Navigate to the productVC
About handling navigation, choose one that fit your project of above these two ways.
If my articles give you any help, it’s my pleasure, and give me some clap, thanks.