Theory Universal Links
Introduction
WWDC15 - Apple announces the move to Universal Links. Their sense is prohibitively simple - to send a user to the application if it goes to the page of the site that the application supports. This is most often used in letters that are sent after registration to confirm e-mail. Thus, Universal Links eliminates the need for the user to return to the application on their own - which is good for both user experience and statistics.
Apple Developer . When registering, make sure to connect the Associated Domains. If the App ID is already registered, simply go to the next item and enable Associated Domains directly from Xcode.
Associated Domains in Xcode

To be clear even for a beginner, we will not do this directly through the .entitlements file and go in a clear way: Xcode → Project → Capabilities → Associated Domains.
Here we need to click on "+" and add domains of interest to us. Domains are necessarily added through the signature applink, for example
applink: habr.com
In this case, we pretend that we want to teach the Habr application to open when switching to one of the hubs, so I boldly point out habr.com. In fact, we will not be able to test Universal Links on Habr, and if you immediately decided to move from theory to practice, then I recommend right now to create a domain on Github Pages and specify it via applink.
AppDelegate.swift
Here it is enough to copy and paste this code, but then there is an explanation for the inquisitive.
func application (_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler:[Any?]-> Void) -> Bool {
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb, let url = userActivity.webpageURL else {return false}
print (url) //Depending on the URL, you can open different application screens.
return true
}
When you run the application through Universal Link, you get an NSUserActivity object with an activityType value. The webpageURL property contains the URL by which the user passed, it can be parsed using NSURLComponents.
NSURLComponents - enough simple theme that has been many times revealed by small projects in the Playground. If you do not understand what it is about, then here's here is a good screenshot .
File apple-app-site-association
It is a file without an extension, but it stores the data in the JSON format. It looks like this.
{
"applinks": {
"apps":[],
"details":[{
"appID": "teamId.com.bundleId.of.your.app",
"paths":["/path-for-redirectionl/*"]
}]
}
}
Also, I strongly ask you right now to fix in memory that this file should be stored in the root of the domain .
In the code we are interested in only two fields. The appID in the apple-app-site-association is the combination of your Team Identifier and the Bundle Identifier. In the code above, I have already substituted the data in such a way that it would be more convenient for you to copy them and change a couple of lines.
How to search for Team Identifier? To do this, go to developer.apple.com → Account → Sign In → Membership. The third field is your Team Identifier. For me it's 74D322Z5HV.
How to search for a Bundle Identifier? Xcode → Project → General. Your Bundle Identifier is written in the Identity section, the second field. For me it is com.habrahabr.ios.application.
Now let's make up an appID for this apple-app-site-association.
"appID": "74D322Z5HV.com.habrahabr.ios.application"
Done! You are gorgeous! Now let's talk about the paths. This field directly to the forehead tells which pages of the site the application supports, which pages you should send to the application. You can add pages as many as you want. Next, a little help on how this works and for you to be even clearer, I remind you that we are making an app for habr.com
"paths":[
"/path-for-redirection/*" //Мы перенесемся в приложение при попадании на URL – habr.com/path-for-redirection/или куда-угодно дальше (за это отвечает *), например при попадании на URL habr.com/path-for-redirection/ok/funny-video-with-dogs/мы всё равно перейдем в приложение.
"/path-for-redirection/" //Мы перенесемся в приложение при попадании на URL – habr.com/path-for-redirection/, но мы убрали звездочку, а значит не перенесемся в приложение при попадании на, например, habr.com/path-for-redirection/ok/.
"*" //Только символ звездочки без указания какого-либо пути значит что мы перенесемся в приложение при попадании вообще на какую угодно страницу сайта.
"NOT /path-for-redirection/*" //Если выше указана звездочка, а следующей строкой следует какой-угодно URL с приставкой NOT – мы перенесемся в приложение при попадании на любую страницу сайта, кроме той, что указана рядом с NOT.
]
And that's what apple-app-site-association looks like if we have, for example, 2 applications: one for the hub "Swift", and the other for the hub "Development under iOS".
{
"applinks": {
"apps":[],
"details":[{
"appID": "74D322Z5HV.com.habrahabr.ios.application.iosdev",
"paths":["/hub/ios_dev/*"]
},
{
"appID": "74D322Z5HV.com.habrahabr.ios.application.swift",
"paths":["/hub/swift/*"]
}]
}
}
Now you can save the apple-app-site-association file without an extension (format) and upload it to the server. Then send the requested URL to your mail, collect in Xcode a project with Associated Domains and code in AppDelegate.swift. To check the work, go to the desired URL from the standard "Mail" application.
Noteworthy facts and advice
Universal Links does not work from the browser, when you go to the desired URL in Safari you will see a dice telling you that you can go to the application.
Universal Links does not work in redirect (redirect) chains. If you hit the desired URL, the site will simply show the top of the page indicating that you can go to the application.
Universal Links can be tested without your own dedicated or virtual server using GitHub Pages.
The URL for the transition must start with "https: //".
You can check whether everything is in order with the apple-app-site-association file through App Search API Validation Tool .
To trigger a transition from an application to the desired URL, you do not need to have any content on this URL. That is, the transition will be made even if the browser on this URL issues 404.
A long press on Universal Link in the mail application will prompt you to choose between opening the application and opening the browser.
References and references
In view of the fact that one of the purposes of this publication is to give maximum understanding to Universal Links, I want to end it with references to the literature that was used to write the article and /or just give a deeper understanding to Universal Links.
Apple Developer Documentation - Allowing Apps and Websites to Link to Your Content
Medium Article - Universal Links in iOS
Medium Article - The things I hate (and you should know) about Apple Universal Links
I hope this publication has become the best that you found on the topic in Russian. In addition, I will be glad to further orient you here (in the comments). If I missed some points or made a mistake, I also ask you to inform me about it.
Thank you!
It may be interesting
This publication has no comments.
Comments
this is really nice to read..informative post is very good to read..thanks a lot! How is the cost of house cleaning calculated?
It’s very informative and you are obviously very knowledgeable in this area. You have opened my eyes to varying views on this topic with interesting and solid content.
entegrasyon programları
entegrasyon programları
I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
entegrasyon programları
entegrasyon programları
I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work...먹튀
Lose Weight Market provides the best fitness tips, workout guides, keto recipes and diet plans, yoga workout routine and plans, healthy recipes, and more! Check Out: Lose Weight Market
Corvus Health provides medical training services as well as recruiting high quality health workers for you or placing our own best team in your facility. Check Out: Health Workforce Recruitment
I.T HATCH offers a wide range of IT services including remote access setup, small business servers, data storage solutions, IT strategy services, and more. Check Out: IT strategy services
Corvus Health provides medical training services as well as recruiting high quality health workers for you or placing our own best team in your facility. Check Out: Health Workforce Recruitment
I.T HATCH offers a wide range of IT services including remote access setup, small business servers, data storage solutions, IT strategy services, and more. Check Out: IT strategy services