Universal link or deep linking in iOS
Lets first understand what is Universal link or deep linking? When and why should we use it in our application?
Let say when you click a link that you received may be from whatsapp or elsewhere, this will open the associated application in your iPhone instead of redirecting it to safari browser if the associated application is installed in your iPhone but if the application is not installed, it will open the link in safari browser in your iPhone.
below steps are used to implement universal links in iOS application :-
1. Configure your app to register approved domains
- Register your app at developer.apple.com.
- Enable Associated Domains on your app identifier.
- Go to Xcode click on your target -> Capabilities.
- Enable ‘Associated Domain’ under Capabilities
- Add the proper domain entitlement and make sure the entitlements file is included at build: Xcode will do it automatically by itself.
- In Associated Domains add applinks:yourdomain.com for example applinks:instagram.com
2. Create the ‘apple-app-site-association’ file aka AASA file
- Create apple-app-site-association file. Here is a sample AASA file format :-
{ “applinks”: { “apps”: [], “details”: [ { “appID”: “JHVFJHHYX.com.xyz.ios”, “paths”: [ “*” ] } ] }}
In the example above, JHVFJHHYX is the Team ID and com.xyz.ios is the Bundle ID.
//app ID = “TeamID: your domain.com”
//Team Id is you developer account team id
//paths = links that refer your domain * will open all the links associated with your domain
Hosting the AASA File on Your Domain
Once you are ready with your AASA file, you can now host it on your domain at https://<<yourdomain>>/apple-app-site-association
Upload the apple-app-site-association
file to your HTTPS web server.
Important: iOS will only attempt to fetch the AASA file over a secure connection (HTTPS).
So, while hosting the AASA file, please ensure that the AASA file:
- Is served over HTTPS.
- Uses application/json MIME type.
- Don’t append
.json
to theapple-app-site-association
filename. - Has a size not exceeding 128 Kb (requirement in iOS 9.3.1 onwards).
AASA VALIDATOR
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
Test
When iOS launches your app after a user taps a universal link, you receive an NSUserActivity
object with an activityType
value of NSUserActivityTypeBrowsingWeb
. The activity object’s webpageURL
property contains the URL that the user is accessing. The webpage URL property always contains an HTTP or HTTPS URL, and you can use NSURLComponents
APIs to manipulate the components of the URL.
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
print("Continue User Activity called: ")
if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
let url = userActivity.webpageURL!
print(url.absoluteString)
//handle url and open whatever page you want to open.
}
return true
}
References — branch.io and Internet Search