Deep Link in iOS App by Universal Link

Jimmy Chen
iOS Funny World

--

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

  1. You have to owner Domain.
  2. Your server have to support HTTPS.
  3. You can upload files to your Web Server .
  4. Your app deployment version have to greater than or equal to iOS 9.
  5. 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 and Web Server .
  • Handle App page navigate when App be open from Universal 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 in Associated Domains at app first install , and app will look for apple-app-site-association file in Web Server to create link between App and Web 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 and BundleID, its format is TeamID.BundleID. You can find TeamID in Apple Developer and find BundleID in Xcode.
  • paths:It’s mean the path you want to support Universal Link , it can be a collection, if you want all path support Universal 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 in iOS 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 before well-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 and SceneDelegate, 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.

--

--