When you support universal links, iOS 9 users can tap a link to your website and get seamlessly redirected to your installed app without going through Safari. If your app isn’t installed, tapping a link to your website opens your website in Safari.
當(dāng)你支持通用鏈接,iOS 9的用戶可以點擊一個鏈接到你的網(wǎng)站并得到無縫地重定向到您安裝的應(yīng)用程序不需要通過Safari。如果你的應(yīng)用程序沒有安裝,點擊一個鏈接到你的網(wǎng)站打開你的網(wǎng)站在Safari。
Universal links give you several key benefits that you don’t get when you use custom URL schemes. Specifically, universal links are:
當(dāng)您使用自定義URL schemes時,您不會得到通用鏈接為您提供了幾個主要優(yōu)點。具體來說,通用鏈接是:
Unique. Unlike custom URL schemes, universal links can’t be claimed by other apps, because they use standard HTTP or HTTPS links to your website.
Secure. When users install your app, iOS checks a file that you’ve uploaded to your web server to make sure that your website allows your app to open URLs on its behalf. Only you can create and upload this file, so the association of your website with your app is secure.
Flexible. Universal links work even when your app is not installed. When your app isn’t installed, tapping a link to your website opens the content in Safari, as users expect.
Simple. One URL works for both your website and your app.
Private. Other apps can communicate with your app without needing to know whether your app is installed.
唯一性。與自定義URL schemes不同,Universal Links不能依靠其他應(yīng)用聲明,因為他們使用標(biāo)準(zhǔn)的HTTP或HTTPS鏈接到您的網(wǎng)站。//擴充:另外,Custom URL scheme 因為是自定義的協(xié)議,所以在沒有安裝 app 的情況下是無法直接打開的,而 universal links 本身是一個 HTTP/HTTPS 鏈接,所以有更好的兼容性
安全性。當(dāng)用戶安裝您的應(yīng)用時,iOS會檢查您上傳到web服務(wù)器的文件,以確保您的網(wǎng)站允許您的應(yīng)用代表其打開網(wǎng)址。 只有您可以創(chuàng)建和上傳此文件,因此您的網(wǎng)站與您的應(yīng)用程序的關(guān)聯(lián)是安全的。
靈活性。即使您的應(yīng)用程序未安裝,Universal Links也會正常工作。當(dāng)您的應(yīng)用未安裝時,點擊一個鏈接到您的網(wǎng)站會像用戶期望的一樣在Safari中打開你網(wǎng)站的內(nèi)容。
簡單性。一個URL鏈接適用于您的網(wǎng)站和您的應(yīng)用程序。
私有性。其他應(yīng)用程序可以與您的應(yīng)用程序通信,而無需知道您的應(yīng)用程序是否已安裝。
NOTE
In iOS 9 and later, universal links let users open your app when they tap links to your website within WKWebView and UIWebView views and Safari pages, in addition to links that result in a call to openURL:, such as those that occur in Mail, Messages, and other apps.
When a user is browsing your website in Safari and they tap a universal link to a URL in the same domain as the current webpage, iOS respects the user’s most likely intent and opens the link in Safari. If the user taps a universal link to a URL in a different domain, iOS opens the link in your app.
For users who are running versions of iOS earlier than 9.0, tapping a universal link to your website opens the link in Safari.
在iOS 9及更高版本中,通用鏈接允許用戶在WKWebView和UIWebView視圖和Safari頁面中點擊指向您網(wǎng)站的鏈接時打開您的應(yīng)用程序,此外也可以在短信、郵件、其他應(yīng)用程序中點擊鏈接打開網(wǎng)頁跳轉(zhuǎn)到應(yīng)用程序。
當(dāng)用戶在Safari中瀏覽您的網(wǎng)站時,他們點擊到與當(dāng)前網(wǎng)頁相同域中的URL的通用鏈接,iOS尊重用戶最可能的意圖,并在Safari中打開鏈接。 如果用戶點擊通向其他域中的URL的鏈接,iOS會在您的應(yīng)用中打開該鏈接。//注釋:如果承載頁和深度鏈接是同一個域名。Universal links失效,無法通過深度鏈接喚起app。
對于運行9.0之前的iOS版本的用戶,點擊指向您網(wǎng)站的通用鏈接將在Safari中打開鏈接。
Adding support for universal links is easy. There are three steps you need to take:
- Create an apple-app-site-association file that contains JSON data about the URLs that your app can handle.
- Upload the apple-app-site-association file to your HTTPS web server. You can place the file at the root of your server or in the .well-known subdirectory.
- Prepare your app to handle universal links.
You can test universal links on a device.
添加對通用鏈接的支持很容易。 您需要采取以下三個步驟:
- 創(chuàng)建一個包含您的應(yīng)用程序可以處理的URL的JSON數(shù)據(jù)的apple-app-site-association文件。
- 將apple-app-site-association文件上傳到HTTPS Web服務(wù)器。 您可以將文件放在服務(wù)器的根目錄或.well-known子目錄中。
- 準(zhǔn)備您的應(yīng)用程序來處理通用鏈接。
您可以測試設(shè)備上的通用鏈接。
Creating and Uploading the Association File
To create a secure connection between your website and your app, you establish a trust relationship between them. You establish this relationship in two parts:
- An apple-app-site-association file that you add to your website
- A com.apple.developer.associated-domains entitlement that you add to your app (this part is described in Preparing Your App to Handle Universal Links)
You can learn more about how your app and website can share credentials in Shared Web Credentials Reference.
在您的網(wǎng)站和應(yīng)用程序之間建立一個安全連接,您需要在它們之間建立信任關(guān)系。 你建立這種關(guān)系分為兩部分:
- 添加apple-app-site-association文件到您的網(wǎng)站
-
您添加到應(yīng)用程序的com.apple.developer.associated-domains entitlement(此部分在準(zhǔn)備應(yīng)用程序以處理通用鏈接中進(jìn)行了說明)//
屏幕快照 2016-12-28 下午4.25.31.png
您可以在“共享網(wǎng)絡(luò)憑據(jù)參考”中詳細(xì)了解您的應(yīng)用和網(wǎng)站如何共享憑據(jù)。
NOTE
If your app runs in iOS 9 or later and you use HTTPS to serve the apple-app-site-association file, you can create a plain text file that uses the application/json MIME type and you don’t need to sign it. If you support Handoff and Shared Web Credentials in iOS 8, you still need to sign the file as described in Shared Web Credentials Reference.
如果您的應(yīng)用程序在iOS 9或更高版本中運行,并且您使用HTTPS來提供apple-app-site-association文件,您可以創(chuàng)建一個使用application / json MIME類型的純文本文件,而不需要對其進(jìn)行簽名。如果您在iOS 8中支持Handoff和Shared Web Credentials,您仍然需要按照共享Web憑據(jù)參考中所述簽名文件。
You need to supply a separate apple-app-site-association file for each domain with unique content that your app supports. For example, apple.com and developer.apple.com need separate apple-app-site-association files, because these domains serve different content. In contrast, apple.com and www.apple.com don’t need separate site association files—because both domains serve the same content—but both domains must make the file available. For apps that run in iOS 9.3.1 and later, the uncompressed size of the apple-app-site-association file must be no greater than 128 KB, regardless of whether the file is signed.
您需要為每個域提供一個單獨的apple-app-site-association文件,其中包含您應(yīng)用支持的唯一內(nèi)容。 例如,apple.com和developer.apple.com需要單獨的apple-app-site-association文件,因為這些域提供不同的內(nèi)容。相比之下,apple.com和www.apple.com不需要單獨的站點關(guān)聯(lián)文件 - 因為兩個域提供相同的內(nèi)容,但兩個域必須使文件可用。 對于在iOS 9.3.1及更高版本中運行的應(yīng)用程序,apple-app-site-association文件的未壓縮大小不得大于128 KB,無論文件是否已簽名。
In your apple-app-site-association file, you specify the paths from your website that should be handled as universal links along with those that should not be handled as universal links. Keep the list of paths 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 handled as universal links.
在您的apple-app-site-association文件中,指定您的網(wǎng)站中應(yīng)作為通用鏈接處理的路徑以及不應(yīng)作為通用鏈接處理的路徑。保持路徑列表相當(dāng)短,并依靠通配符匹配來匹配更大的路徑集。 清單6-1顯示了一個apple-app-site-association文件的示例,它標(biāo)識了應(yīng)該作為通用鏈接處理的三個路徑。

NOTE
Don’t append .json to the apple-app-site-association filename.
不要將.json追加到apple-app-site-association文件名。(在構(gòu)建應(yīng)用程序后,appID值與應(yīng)用程序權(quán)限中的“application-identifier”鍵相關(guān)聯(lián)。)
The apps key in an apple-app-site-association file must be present 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 dictionary per app that your website supports. The order of the dictionaries in the array determines the order the system follows when looking for a match, so you can specify an app to handle a particular part of your website.
apple-app-site-association文件中的apps鍵必須存在,其值必須為空數(shù)組,如清單6-1所示。 details鍵的值是一個字典數(shù)組,您的網(wǎng)站支持的每個應(yīng)用程序一個字典。 數(shù)組中字典的順序決定了系統(tǒng)在查找匹配時所遵循的順序,因此您可以指定一個應(yīng)用程序來處理網(wǎng)站的特定部分。
Each app-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 same value that’s associated with the “application-identifier” key in your app’s entitlements after you build it.) The value of the paths key is an array of strings that specify the parts of your website that are supported by the app and the parts of your website that you don’t want to associate with the app. To specify an area that should not be handled as a universal link, add “NOT ” (including a space after the T) to the beginning of the path string. For example, the apple-app-site-association file shown in Listing 6-1 could prevent the /videos/wwdc/2010/* area of the website from being handled as a universal link by updating the paths array as shown here:
每個應(yīng)用程序特定的字典包含一個appID鍵和一個paths鍵。 appID鍵的值是team ID 或app ID前綴,后跟bundleID。paths鍵的值是一個字符串?dāng)?shù)組,指定您的網(wǎng)站的應(yīng)用程序和您不想與應(yīng)用程序關(guān)聯(lián)的網(wǎng)站部分支持的部分。 要指定不應(yīng)作為通用鏈接處理的區(qū)域,請將“NOT ”(包括T后的空格)添加到路徑字符串的開頭。 例如,如清單6-1所示的apple-app-site-association文件可以通過更新paths數(shù)組來阻止網(wǎng)站的/ videos / wwdc / 2010 / *區(qū)域被作為通用鏈接處理,如下所示:

Because the system evaluates each path in the paths array in the order it is specified—and stops evaluating when a positive or negative match is found—you should specify high priority paths before low priority paths. Note that only the path component of the URL is used for comparison. Other components, such as the query string or fragment identifier, are ignored.
因為系統(tǒng)按照它指定的順序評估paths數(shù)組中的每個路徑,并在找到正或負(fù)匹配時停止計算,因此您應(yīng)在低優(yōu)先級路徑之前指定高優(yōu)先級路徑。 請注意,只有URL的路徑組件用于比較。 忽略其他組件,例如查詢字符串或片段標(biāo)識符。
There are various ways to specify website paths in the apple-app-site-association file. For example, you can:
- Use * to specify your entire website
- Include a specific URL, such as /wwdc/news/, to specify a particular link
- Append * to a specific URL, such as /videos/wwdc/2015/, to specify a section of your website.
In addition to using * to match any substring, you can also use ? to match any single character. You can combine both wildcards in a single path, such as /foo//bar/201?/mypage.
有多種方法在apple-app-site-association文件中指定網(wǎng)站路徑。 例如,您可以:
- 使用*指定整個網(wǎng)站
- 包括特定URL,例如/ wwdc / news /,以指定特定鏈接
- 將*附加到特定網(wǎng)址,例如/ videos / wwdc / 2015 / ,以指定您網(wǎng)站的某個部分。除了使用匹配任何子字符串,還可以使用? 匹配任何單個字符。 您可以在單個路徑中組合這兩個通配符,例如/ foo / * / bar / 201?/ mypage
NOTE
The strings you use to specify website paths in the paths array are case sensitive.
用于在paths數(shù)組中指定網(wǎng)站路徑的字符串對大小寫是敏感的。//即:區(qū)分大小寫。
After you create the apple-app-site-association file, upload it to the root of your HTTPS web server or to the .well-known subdirectory. The file needs to be accessible via HTTPS—without any redirects—at https://<domain>/apple-app-site-association or https://<domain>/.well-known/apple-app-site-association. Next, you need to handle universal links in your app.
創(chuàng)建apple-app-site-association文件后,將其上傳到HTTPS Web服務(wù)器的根目錄或.well-known子目錄。 該文件需要通過HTTPS訪問(無需任何重定向),網(wǎng)址為https:// <domain> / apple-app-site-association或https:// <domain> /.well-known/apple-app-site-association 。 接下來,您需要處理應(yīng)用程序中的通用鏈接。
Preparing Your App to Handle Universal Links
Universal links use two technologies: The first is the same mechanism that powers Handoff between a web browser and a native app, and the second is Shared Web Credentials (for more information about these technologies, see Web Browser–to–Native App Handoff and Shared Web Credentials Reference). When a user taps a universal link, iOS launches your app and sends it an NSUserActivity object that you can query to find out how your app was launched.
Universal links使用兩種技術(shù):第一種是在Web瀏覽器和原生應(yīng)用程序之間切換的相同機制,第二種是共享Web憑據(jù)(有關(guān)這些技術(shù)的更多信息,請參閱Web瀏覽器到本機應(yīng)用程序切換和共享 Web憑據(jù)參考)。 當(dāng)用戶點擊universal link時,iOS會啟動您的應(yīng)用程序,并向其發(fā)送一個NSUserActivity對象,您可以查詢該對象以了解您的應(yīng)用程序是如何啟動的。
To support universal links in your app, take the following steps:
- Add an entitlement that specifies the domains your app supports.
- Update your app delegate to respond appropriately when it receives the NSUserActivity object.
要在您的應(yīng)用中支持universal links,請執(zhí)行以下步驟:
- 添加指定您的應(yīng)用支持的域的權(quán)利。
- 更新應(yīng)用程序委派以在接收到NSUserActivity對象時進(jìn)行適當(dāng)響應(yīng)。
In your com.apple.developer.associated-domains entitlement, include a list of the domains that your app wants to handle as universal links. To do this in Xcode, open the Associated Domains section in the Capabilities tab and add an entry for each domain that your app supports, prefixed with applinks:, such as applinks:www.mywebsite.com. Limit this list to no more than about 20 to 30 domains.
在com.apple.developer.associated-domains entitlement中,包含您的應(yīng)用程序要作為通用鏈接處理的域的列表。 要在Xcode中執(zhí)行此操作,請打開“ Capabilities”選項卡中的“Associated Domains”部分,并為應(yīng)用程序支持的每個域添加一個條目,并以applinks:為前綴,如applinks:www.mywebsite.com。 將此列表限制為不超過大約20到30個域。
To match all subdomains of an associated domain, you can specify a wildcard by prefixing . before the beginning of a specific domain (the period is required). Domain matching is based on the longest substring in the applinks entries. For example, if you specify the entries applinks:.mywebsite.com and applinks:*.users.mywebsite.com, matching for the domain emily.users.mywebsite.com is performed against the longer *.users.mywebsite.com entry. Note that an entry for *.mywebsite.com does not match mywebsite.com because of the period after the asterisk. To enable matching for both *.mywebsite.com and mywebsite.com, you need to provide a separate applinks entry for each.
要匹配associated domain的所有子域,您可以通過使用前綴指定通配符。 在特定域的開始之前(該期間是必需的)。 域匹配基于applinks條目中的最長子字符串。 例如,如果指定條目applinks:。mywebsite.com和applinks:。users.mywebsite.com,則對域emily.users.mywebsite.com的匹配將針對較長的 .users.mywebsite.com條目執(zhí)行 。 請注意,* .mywebsite.com的條目與mywebsite.com不匹配,因為星號后的時間段。 要為* .mywebsite.com和mywebsite.com啟用匹配,您需要為每個都提供單獨的applinks條目。
After you specify your associated domains, adopt the UIApplicationDelegate methods for Handoff (specifically application:continueUserActivity:restorationHandler:) so that your app can receive a link and handle it appropriately.
指定關(guān)聯(lián)的域后,為Handoff采用UIApplicationDelegate方法(具體應(yīng)用程序:continueUserActivity:restorationHandler :),以便應(yīng)用程序可以接收鏈接并適當(dāng)處理。
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.
當(dāng)iOS在用戶點擊universal link后啟動應(yīng)用程序時,您將收到一個activityType值為NSUserActivityTypeBrowsingWeb的NSUserActivity對象。 活動對象的webpageURL屬性包含用戶正在訪問的網(wǎng)址。 這個webpage URL屬性始終包含HTTP或HTTPS URL,您可以使用NSURLComponents API來處理URL的組件。
When a user taps a universal link that you handle, iOS also examines the user’s recent choices to determine whether to open your app or your website. For example, a user who has tapped a universal link to open your app can later choose to open your website in Safari by tapping a breadcrumb button in the status bar. After the user makes this choice, iOS continues to open your website in Safari until the user chooses to open your app by tapping OPEN in the Smart App Banner on the webpage.
當(dāng)用戶點按您處理的universal link時,iOS還會檢查用戶最近的選擇,以確定是打開您的應(yīng)用還是您的網(wǎng)站。 例如,點擊universal link以打開應(yīng)用的用戶可以稍后通過點擊狀態(tài)欄中的導(dǎo)航欄按鈕在Safari中選擇打開您的網(wǎng)站。 在用戶做出此選擇之后,iOS會繼續(xù)在Safari中打開您的網(wǎng)站,直到用戶通過在網(wǎng)頁上的智能應(yīng)用橫幅中點擊打開來選擇打開您的應(yīng)用。
NOTE
If you instantiate a SFSafariViewController, WKWebView, or UIWebView object to handle a universal link, iOS opens your website in Safari instead of opening your app. However, if the user taps a universal link from within an embedded SFSafariViewController, WKWebView, or UIWebView object, iOS opens your app.
如果您實例化SFSafariViewController,WKWebView或UIWebView對象來處理通用鏈接,iOS會在Safari中打開您的網(wǎng)站,而不是打開您的應(yīng)用程序。 但是,如果用戶從嵌入式SFSafariViewController,WKWebView或UIWebView對象中輕擊通用鏈接,iOS會打開您的APP。
It’s important to understand that if your app uses openURL: to open a universal link to your website, the link always opens in Safari instead of getting redirected to your app. In this scenario, iOS recognizes that the call originates from your app and therefore should not be handled as a universal link.
請務(wù)必了解,如果您的應(yīng)用使用openURL:打開指向您網(wǎng)站的通用鏈接,該鏈接將始終在Safari中打開,而不是重定向到您的應(yīng)用。 在這種情況下,iOS會識別呼叫來自您的應(yīng)用程序,因此不應(yīng)作為通用鏈接處理。
If you receive an invalid URL in an activity object, it’s important to fail gracefully. To handle an unsupported URL, you can call openURL: on the shared application object to open the link in Safari. If you can’t make this call, display an error message to the user that explains what went wrong.
如果您在活動對象中收到無效的網(wǎng)址,請務(wù)必妥善處理失敗。 要處理不受支持的URL,可以在共享應(yīng)用程序?qū)ο笊险{(diào)用openURL:以在Safari中打開鏈接。 如果您無法進(jìn)行此調(diào)用,請向用戶顯示一條錯誤消息,說明發(fā)生了什么問題。
IMPORTANT
To protect users’ privacy and security, you should not use HTTP when you need to transport data; instead, use a secure transport protocol such as HTTPS.
為了保護用戶的隱私和安全,當(dāng)您需要傳輸數(shù)據(jù)時,不應(yīng)使用HTTP; 而是使用安全傳輸協(xié)議(如HTTPS)。
關(guān)于universal link相關(guān)的文章
Breaking down iOS 9 Universal Links
Android M App Links: implementation, drawbacks and solutions
