Manage Universal Links with Multiple Associated Domains in iOS
Probably if you are here, you have manage deep links before but you know that you could manage links in a better way, so here we are, let’s do it.
Universal links give us a clean way to set links to open an app in android or iOS. To apply Universal Links that works in iOS it is necessary to set up the associated domains in our project and website.
“Associated domains establish a secure association between domains and your app so you can share credentials or provide features in your app from your website.” — https://developer.apple.com/documentation/safariservices/supporting_associated_domains
Imagine that we are going to set up a scenario when we manage 3 environments, so we are going to prepare the project with 3 associated domains, these are the base links that will open our app (when user has it installed)
* Develop, https:\\develop.example.com
* QA, https:\\qa.example.com
* Production, https:\\example.com
Let’s start adding this associated domains to our iOS app. Open the project Signing & Capabilities tab in Xcode, then add each Associated Domain like this:
Remove ‘https:\\’ in each url and add it with this format: ‘applinks:yourDomain.com’.
Now we could go to Entitlements file ‘Example.entitlements’ and check that our applinks are set up, it could look like this:
<key>com.apple.developer.associated-domains</key>
<array>
<string>applinks:develop.example.com</string><string>applinks:qa.example.com</string><string>applinks:example.com</string>
</array>
Now that we have this ready could go and set up the Associated Domains in the website, create a file named apple-app-site-association
(without an extension), after this place it in .well-known
directory of your website.
Remember that we have 3 environments for this case so we need to put this file in each one:
https://develop.example.com/.well-known/apple-app-site-association
https://qa.example.com/.well-known/apple-app-site-association
https://example.com/.well-known/apple-app-site-association
This file contain details related to our app, so the website know about the app. We are going to associated the app TeamId.BundleID with applinks in this file, replace the values of TeamID and BundleID in the appID value, then in paths we are going to limit the paths for what we expect to receive : “/open-coupon/*”
For simplicity, you use the * to associate all of this web site’s links with the UniversalLinks app.
In this case we are supposing that going to open a link to load a coupon, so user will receive a link like this: https:\\example.com\open-coupon?id=1234 .
The last thing that we need to do is update our AppDelegate to manage the information that Universal link give us as the ‘id’ value
As you see it’s easy to configure and you could offer a stylish way to open your app. Hope this could be useful :)