
Introduction
WWDC15 - Apple announces transition to Universal Links. Their meaning is prohibitively simple - send the user to the application if he goes to the page of the site that the application supports. This is most often used in emails that are sent after registration to confirm email. Thus, Universal Links eliminates the need for the user to return to the application on his own - which is good for both user experience and statistics.

Many may now think that this article is not being written on time, but you, dear reader, may not have seen what is happening on Stackoverflow on the topic of Universal Links.
')
Implementation
Now we will examine four points that will allow you to implement Universal Links without the extra pain and blinding surprises that I had to face. In the end, we easily touch on other literature and links that may give you a deeper understanding of Universal Links.
Let's move on to the first item.
Associated Domains in App IDs

If you have not registered the App ID for your application, then do it now on the
Apple Developer website. When registering, make sure you connect the Associated Domains. If the App ID is already registered, simply skip to the next item and enable Associated Domains directly from Xcode.
Associated Domains in Xcode

To be clear even to a beginner, we will not do this directly through the .entitlements file and go clear: Xcode → Project → Capabilities → Associated Domains.
Here we need to click on the "+" and add the domains of interest. Domains must be added through the applink signature, 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 to habr.com. In fact, we will not be able to test the work of Universal Links on Habré, and if you immediately decided to move from theory to practice, I recommend right now to create a domain on Github Pages and specify it via applink.
AppDelegate.swift
Here we just need to copy and paste this code, but then comes the 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)
When you run the application through Universal Link, you get an NSUserActivity object with the value activityType. The webpageURL property contains the URL that the user went to; it can be broken apart using NSURLComponents.
NSURLComponents is a fairly simple topic that was uncovered many times in small projects in the Playground. If you do not understand what it is about, then
here is a good screenshot .
Apple-app-site-association file
It is a file without an extension, but it stores data in JSON format. It looks something like this.
{ "applinks": { "apps": [], "details": [{ "appID": "teamId.com.bundleId.of.your.app", "paths": ["/path-for-redirectionl/*"] }] } }
Also, I urge you right now to fix in memory that this file should
be stored in the root directory of the domain .
In the code, we are in fact only interested in two fields. The appID in the apple-app-site-association is a combination of your Team Identifier and 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 it and change a couple of lines.
How to search for Team Identifier? To do this, go to the site developer.apple.com → Account → Sign In → Membership. The third field is your Team Identifier. For me, this is 74D322Z5HV.
How to search for a Bundle Identifier? Xcode → Project → General. Your Bundle Identifier is written in the Identity section, second field. For me, this is com.habrahabr.ios.application.
Now let's compose from this appID for apple-app-site-association.
"appID": "74D322Z5HV.com.habrahabr.ios.application"
Done! You are gorgeous! Now let's talk about paths. This field tells you directly which pages of the site the application supports, from which pages you should be sent to the application. You can add pages as you like. Next, a little help on how it works and to make it even more clear to you, I remind you that we are making an application 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. ]
But apple-app-site-association will look like this if, for example, we have 2 applications: one for the Swift hub and one for the iOS Development hub.
{ "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 required URL to your email, compile the project in Xcode with Associated Domains and the code in AppDelegate.swift. Check the work should go to the desired URL from the standard application "Mail".
Remarkable Facts and Tips
- Universal Links does not work from the browser; when you go to the required URL in Safari, you will be shown a dummy telling you that you can go to the application.
- Universal Links does not work in redirects (redirects). When you hit the desired URL, the site will simply show at the top of the plate, indicating that you can go into the application.
- Universal Links can be tested without your own dedicated or virtual server using GitHub Pages.
- The transition URL must begin with "https: //".
- Check whether everything is in order with the file apple-app-site-association is possible through the App Search API Validation Tool .
- To cause a transition to the application from the desired URL, it is not necessary to have any content at this URL. That is, the transition will be made even if the browser at this URL gives 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 used literature
In view of the fact that one of the goals of this publication is to give a maximum understanding of Universal Links, I want to complete it with references to the literature that was used in writing the article and / or simply gives a deeper understanding of 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 I was mistaken about something, I also ask you to let me know about it.
Thank!