Universal Links in iOS

Do you want to enable your users to launch your app directly from a click on a shared link? Universal link on iOS (or deep links) is any link that directs a user past the home page of a website or app to the content inside of it. Here is a simplified step-by-step tutorial for implementing such a link.

This tutorial assumes that you have an App Store account and you know how to register App IDs, and you also have a web domain where you can upload files.

Step 1: Configuring your app to receive app links.

Before you begin, Register your App ID in your App Store Account. During registration of App ID, check Associated Domains under Capabilities.

In XCode project section, select Capabilities and turn on Associated Domains.

Next, add an item under domains, in the format applinks:yourwebsite.com where yourwebsite.com is your website domain.

At this step, you should see two automatically checkmarked steps under the domain list, as shown below.

Your app is now configured to receive universal links.

2. The AASA file

Create a new file called apple-app-site-association on your desktop, and add the following content:

{
  "applinks": {
    "apps": [],
    "details": [
    {
      "appID": "KFFNVC27GU.com.yourcompany.MyProject",
      "paths": ["*"]
    }
    ]
  }
}

The appID here consists of your team ID combined with the app’s bundle ID.

Team ID

You’ll find this in your Apple developer account. Go to Apple developer center. Log into the web site, click on Membership, then look for Team ID in the Membership Information section.

Bundle ID

You’ll find this under the General tab in your project settings in XCode.

Once this AASA file is created, just host it in the root folder of your domain.

Some points to keep in mind while hosting the AASA file:

  • It should be served over HTTPS
  • Be sure to define a ‘content-type’ header with application/json MIME type.
  • Don’t append .json to the apple-app-site-association filename.
  • Size of the file shouldn’t exceed 128 Kb.

After hosting, you must be able to see the json text when you open the following URL in your browser.

https://yourwebsite.com/apple-app-site-association

(Optional) You can validate this step using a tool by branch.io.

Goto the following link and enter your domain name. It will check if AASA file is valid and is accessible.

Link: https://branch.io/resources/aasa-validator/#resultsbox

3. Handling links received in the app

If everything went well, you will now receive universal links in your app, so let us see how to handle them.

AppDelegate.swift

In your AppDelegate, add the following delegate function; it is called upon receiving an app link.

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {

        guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
            let url = userActivity.webpageURL, let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else {
                return false
            }


        //FOR A URL "https://yourwebsite.com/testing/24
        //this will print the ID 24
        
        if (components.path.contains("testing")) {
            if let theid = Int(url.lastPathComponent) {
                print("test id from deep link \(theid)")
            }
        }
        
        
        return false
}


Also published on Medium.

By |2019-11-27T05:48:06+00:00November 27th, 2019|Categories: iOS, iOS Tutorials|0 Comments

Leave A Comment