When you support universal links, iOS users can click a link to your website and be seamlessly redirected to the installed app without having to go through Safari. If your app isn't installed, click your website's link to open your website in Safari.
Universal links give you several major advantages that you don't get when using a custom URL scheme. Specifically, universal links are:
Unique. Unlike custom URL schemes, other apps cannot declare universal links because they use standard HTTP or HTTPS to link to your site.
Safe. When a user installs your app, iOS checks the file you've uploaded to your web server to make sure your website allows your app to open URLs on its behalf. Only you can create and upload this file, so your website's association with your application is secure.
Flexible. Universal links work even if your application is not installed. When your app is not installed, clicking a link to your website opens the content in Safari as the user expects.
Simple. One URL works for your website and your application.
Private. Other applications can communicate with your application without knowing whether your application is installed.
Note
Universal links allow users to open your app before clicking a link to your website in WKWebView, UIWebView and Safari pages, as well as links that result in a call to openURL:, e.g. Links that occur in Mail, Mail and other applications.
When a user is browsing your site in Safari and clicks a universal link to a URL that is in the same domain as the current webpage, iOS respects the user's most likely intent and opens the link in Safari . If a user clicks on a universal link with a URL in a different domain, iOS opens the link in your app.
For users running iOS versions prior to 9.0, clicking a universal link to your website will open the link in Safari.
Adding support for universal links is easy. You need to take the following three steps:
Create an apple-app-site-association file containing JSON data for URLs that your application can handle.
Upload the apple-app-site-association file to your HTTPS web server. You can place the file in the server's root directory or in a .well-known subdirectory.
Prepare your application to handle universal links.
You can test universal links on your device.
Create and upload association files
To establish a secure connection between your website and your application, you will establish a trust relationship between them. You establish this relationship in two parts:
the apple-app-site-association file you add to your site
the com.apple.developer.associated-domains you Adding entitlements to your application (described in this section Preparing your application to handle universal links)
You can learn more about how applications and websites share web credentials in the " *** Share Web Credentials Reference More information on China's shared credentials.
Note
If your app is running in iOS 9 or later and uses HTTPS to serve the apple-app-site-association file, you can create an application using /jsonMIME type plain text file without signing it. If you have support for switching and sharing network credentials in iOS 8, you still need to sign the file as described in Shared Web Credentials Reference.
You will need to provide separate files for each domain of the apple-app-site-association unique content your application supports. For example, apple.com and developer.apple.com require separate apple-app-site-association files because these domains serve different content. In contrast, apple.com and www.apple.com do not require separate site association files - since both domains serve the same content - but both domains must make the file available. For apps running in iOS 9.3.1 and later, the uncompressed size of the apple-app-site-association file must be no larger than 128 KB, regardless of whether the file is signed.
In your apple-app-site-association file, you will specify the paths in your site that should be treated as universal links and the paths that should not be treated as universal links. Keep path lists fairly short, and rely on wildcard matching to match larger sets of paths. Listing 6-1 shows an example of an apple-app-site-association file that identifies three paths that should be treated as universal links.
Listing 6-1 Creating the apple-app-site-association file
{
"applinks": {
"apps" : [],
"details": [
{
"appID": "9JA89QQLNQ.com.apple.wwdc",
< p> "paths": [ "/wwdc/news/", "/videos/wwdc/2015/*"]},
{
"appID": "ABCD1234.com.apple.wwdc",
"paths": [ "*" ]
}
]
}
}
Note
Do not append .json to the apple-app-site-association file name.
The apps key apple-app-site-association in the file must exist, and its value must be an empty array, as shown in Listing 6-1. The value of the details key is an array of dictionaries, one for each application your website supports. The order of the dictionaries in the array determines the order the system follows when looking for matches, so you can specify an application to handle specific parts of your website.
Each application-specific dictionary contains an appID key and a paths key. The value of the appID key is the team ID or app ID prefix, followed by the bundle ID. (The appID value is the value associated with the "Application Identifier" key in the entitlement on which the app is built.) The value of the paths key is an array of strings specifying the portions of websites that the app supports and the websites that you do not want to be associated with the app part. To specify a region that should not be treated as a universal link, add "NOT" (including a space T) to the beginning of the path string. For example, the file shown in Listing 6-1 apple-app-site-association may prevent the /videos/wwdc/2010/* zone from being treated as a universal link by updating the paths array as follows:
< p> "paths": [ "/wwdc/news/", "NOT /videos/wwdc/2010/*", "/videos/wwdc/201?/*"]Because the system paths follow Each path in the array is evaluated in the specified order, and evaluation stops when a positive or negative match is found - high-priority paths should be specified before low-priority paths. Note that only the path component of the URL is used for comparison. Other components such as query strings or fragment identifiers are ignored.
There are several ways to specify the website path in the apple-app-site-association file. For example, you can:
Specify the entire website using *
Include a specific URL, such as /wwdc/news/ Specify a specific link
Append * To a specific URL, such as /videos/wwdc/2015/*, specify part of your website
In addition to using * to match any substring, you can also use ? to match any single character. You can combine two wildcard characters in a single path, such as /foo/*/bar/201?/mypage.
Note
The string used to specify the website path in the paths array is case-sensitive.
After creating the apple-app-site-association file, upload it to the root directory or .well-known subdirectory of your HTTPS web server. The file needs to be accessed via HTTPS, without any redirects, and in the .apple.developer.associated-domains entitlement, list the domains to be handled by the application as universal links. To do this in Xcode, open the "Associated Domains" section in the Features tab and add an entry for each domain your app supports, prefixed with applinks: such as applinks:www.mywebsite.com. Limit this list to no more than about 20 to 30 domains.
To match all subdomains of the associated domain, you can specify a wildcard character by prefixing *. before the beginning of the specific domain (requires the period). Domain matching is based on the longest substring in the applinks entry. For example, if you specify the entry applinks:*.mywebsite.com and perform a domain match on longer entries applinks:*.users.mywebsite.com. Note that the entries do not match due to the period after the asterisk. In order to make two match sums, you need to provide a separate entry for each. emily.users.mywebsite.com*.users.mywebsite.com*.mywebsite.commywebsite.com*.mywebsite.commywebsite.comapplinks
After specifying the associated domain, use UIApplicationDelegate Handoff (specifically application: continueUserActivity:restorationHandler: ) method so that your application can receive the link and handle it gracefully.
When iOS launches your app after the user clicks a universal link, you receive an NSUserActivity object with an activityType value of NSUserActivityTypeBrowsingWeb . The activity object's webpageURL property contains the URL the user is visiting. The page URL attribute always contains an HTTP or HTTPS URL, and you can use the NSURLComponents API to manipulate the URL's components.
When a user clicks on a universal link that you handle, iOS also checks the user's recent selections to determine whether to open your app or your website. For example, a user who opens an app using a universal link can later click the breadcrumb button in the status bar and choose to open your website in Safari. After the user makes this selection, iOS continues to open your website in Safari until the user chooses to open your app by clicking OPEN in the smart app banner on the web page.
Note
If you instantiate an SFSafariViewController , WKWebView or UIWebView object to handle universal links, iOS will open your website in Safari instead of opening your app. However, if the user clicks on a link embedded in a universal SFSafariViewController, WKWebView or UIWebView object, the iOS version opens the app.
It is important to understand that if your application uses openURL: to open a universal link to your website, the link will not be opened in your application. In this case, iOS recognizes that the call originates from your app, so your app should not be considered a universal link.
If you receive an invalid URL in the activity object, it is important to fail gracefully. To handle unsupported URLs, you can call openURL: on the shared application object to open the link in Safari. If you are unable to make this call, an error message will be displayed to the user explaining what happened.
Important
To protect the privacy and security of your users, you do not have to use HTTP when you need to transfer data; instead, use a secure transfer protocol such as HTTPS.