
Apple introduced Universal Links in iOS 9 as a solution to the lack of graceful fallback functionality in custom URI scheme deep links. Universal Links are standard web links (http://mydomain.com) that point to both a web page and a piece of content inside an app. When a Universal Link is opened, iOS checks to see if any installed app is registered for that domain. If so, the app is launched immediately without ever loading the web page. If not, the web URL (which can be a simple redirect to the App Store) is loaded in Safari.
The AASA (short for apple-app-site-association) is a file that lives on your website and associates your website domain with your native app. In other words, it’s a safe way to prove domain ownership to iOS. With URI schemes, which were the standard way for opening apps on iOS until iOS 9, app developers could register any URI scheme of their liking and iOS, without any verification, would respond to those URI schemes by opening apps. For example, if some developer registers the fb:// URI scheme for a test app, there was nothing to stop that, even thoughfb:// is used by the Facebook native app. The AASA file makes Universal Links unique and secure because there is no way for other developers to host an AASA file on the facebook.com domain.
Let’s look at some basics of the apple-app-site-association file that will help you in building and hosting one on your domain.
The AASA file contains a JSON object with a list of apps and the URL paths on the domain that should be included or excluded as Universal Links. Here is a sample AASA file:
{
"applinks": {
"apps": [],
"details": [
{
"appID": "KFFNVC27GU.com.yourcompany.YourProject",
"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:
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 (Example:- https://facebook.com/apple-app-site-association)
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
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) -> Boo
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb
let url = userActivity.webpageURL, let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else {
return false
}
debugPrint("URL==\(userActivity.webpageURL?.absoluteString)")
return false
}
Output
Reference:- https://developer.apple.com/library/archive/documentation/General/Conceptual/AppSearch/UniversalLinks.html
That's all folks!