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的用戶可以點(diǎn)擊一個(gè)鏈接到你的網(wǎng)站并得到無(wú)縫地重定向到您安裝的應(yīng)用程序不需要通過(guò)Safari。如果你的應(yīng)用程序沒(méi)有安裝帝嗡,點(diǎn)擊一個(gè)鏈接到你的網(wǎng)站打開(kāi)你的網(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時(shí)氓润,您不會(huì)得到通用鏈接為您提供了幾個(gè)主要優(yōu)點(diǎn)。具體來(lái)說(shuō),通用鏈接是:
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)用聲明马篮,因?yàn)樗麄兪褂脴?biāo)準(zhǔn)的HTTP或HTTPS鏈接到您的網(wǎng)站。//擴(kuò)充:另外,Custom URL scheme 因?yàn)槭亲远x的協(xié)議怜奖,所以在沒(méi)有安裝 app 的情況下是無(wú)法直接打開(kāi)的浑测,而 universal links 本身是一個(gè) HTTP/HTTPS 鏈接,所以有更好的兼容性
安全性烦周。當(dāng)用戶安裝您的應(yīng)用時(shí)尽爆,iOS會(huì)檢查您上傳到web服務(wù)器的文件,以確保您的網(wǎng)站允許您的應(yīng)用代表其打開(kāi)網(wǎng)址读慎。 只有您可以創(chuàng)建和上傳此文件漱贱,因此您的網(wǎng)站與您的應(yīng)用程序的關(guān)聯(lián)是安全的。
靈活性夭委。即使您的應(yīng)用程序未安裝幅狮,Universal Links也會(huì)正常工作。當(dāng)您的應(yīng)用未安裝時(shí)株灸,點(diǎn)擊一個(gè)鏈接到您的網(wǎng)站會(huì)像用戶期望的一樣在Safari中打開(kāi)你網(wǎng)站的內(nèi)容崇摄。
簡(jiǎn)單性。一個(gè)URL鏈接適用于您的網(wǎng)站和您的應(yīng)用程序慌烧。
私有性逐抑。其他應(yīng)用程序可以與您的應(yīng)用程序通信,而無(wú)需知道您的應(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頁(yè)面中點(diǎn)擊指向您網(wǎng)站的鏈接時(shí)打開(kāi)您的應(yīng)用程序进每,此外也可以在短信、郵件命斧、其他應(yīng)用程序中點(diǎn)擊鏈接打開(kāi)網(wǎng)頁(yè)跳轉(zhuǎn)到應(yīng)用程序田晚。
當(dāng)用戶在Safari中瀏覽您的網(wǎng)站時(shí),他們點(diǎn)擊到與當(dāng)前網(wǎng)頁(yè)相同域中的URL的通用鏈接国葬,iOS尊重用戶最可能的意圖贤徒,并在Safari中打開(kāi)鏈接。 如果用戶點(diǎn)擊通向其他域中的URL的鏈接汇四,iOS會(huì)在您的應(yīng)用中打開(kāi)該鏈接接奈。//注釋:如果承載頁(yè)和深度鏈接是同一個(gè)域名。Universal links失效船殉,無(wú)法通過(guò)深度鏈接喚起app鲫趁。
對(duì)于運(yùn)行9.0之前的iOS版本的用戶,點(diǎn)擊指向您網(wǎng)站的通用鏈接將在Safari中打開(kāi)鏈接利虫。
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.
添加對(duì)通用鏈接的支持很容易挨厚。 您需要采取以下三個(gè)步驟:
- 創(chuàng)建一個(gè)包含您的應(yīng)用程序可以處理的URL的JSON數(shù)據(jù)的apple-app-site-association文件。
- 將apple-app-site-association文件上傳到HTTPS Web服務(wù)器糠惫。 您可以將文件放在服務(wù)器的根目錄或.well-known子目錄中疫剃。
- 準(zhǔn)備您的應(yīng)用程序來(lái)處理通用鏈接。
您可以測(cè)試設(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)用程序之間建立一個(gè)安全連接巢价,您需要在它們之間建立信任關(guān)系。 你建立這種關(guān)系分為兩部分:
- 添加apple-app-site-association文件到您的網(wǎng)站
-
您添加到應(yīng)用程序的com.apple.developer.associated-domains entitlement(此部分在準(zhǔn)備應(yīng)用程序以處理通用鏈接中進(jìn)行了說(shuō)明)//
您可以在“共享網(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或更高版本中運(yùn)行壤躲,并且您使用HTTPS來(lái)提供apple-app-site-association文件,您可以創(chuàng)建一個(gè)使用application / json MIME類型的純文本文件备燃,而不需要對(duì)其進(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.
您需要為每個(gè)域提供一個(gè)單獨(dú)的apple-app-site-association文件漏麦,其中包含您應(yīng)用支持的唯一內(nèi)容。 例如况褪,apple.com和developer.apple.com需要單獨(dú)的apple-app-site-association文件撕贞,因?yàn)檫@些域提供不同的內(nèi)容。相比之下测垛,apple.com和www.apple.com不需要單獨(dú)的站點(diǎn)關(guān)聯(lián)文件 - 因?yàn)閮蓚€(gè)域提供相同的內(nèi)容捏膨,但兩個(gè)域必須使文件可用。 對(duì)于在iOS 9.3.1及更高版本中運(yùn)行的應(yīng)用程序食侮,apple-app-site-association文件的未壓縮大小不得大于128 KB号涯,無(wú)論文件是否已簽名熬北。
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)短,并依靠通配符匹配來(lái)匹配更大的路徑集起胰。 清單6-1顯示了一個(gè)apple-app-site-association文件的示例久又,它標(biāo)識(shí)了應(yīng)該作為通用鏈接處理的三個(gè)路徑。
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鍵的值是一個(gè)字典數(shù)組戒劫,您的網(wǎng)站支持的每個(gè)應(yīng)用程序一個(gè)字典半夷。 數(shù)組中字典的順序決定了系統(tǒng)在查找匹配時(shí)所遵循的順序,因此您可以指定一個(gè)應(yīng)用程序來(lái)處理網(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:
每個(gè)應(yīng)用程序特定的字典包含一個(gè)appID鍵和一個(gè)paths鍵巫橄。 appID鍵的值是team ID 或app ID前綴,后跟bundleID茵典。paths鍵的值是一個(gè)字符串?dāng)?shù)組湘换,指定您的網(wǎng)站的應(yīng)用程序和您不想與應(yīng)用程序關(guān)聯(lián)的網(wǎng)站部分支持的部分。 要指定不應(yīng)作為通用鏈接處理的區(qū)域统阿,請(qǐng)將“NOT ”(包括T后的空格)添加到路徑字符串的開(kāi)頭彩倚。 例如,如清單6-1所示的apple-app-site-association文件可以通過(guò)更新paths數(shù)組來(lái)阻止網(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.
因?yàn)橄到y(tǒng)按照它指定的順序評(píng)估paths數(shù)組中的每個(gè)路徑帆离,并在找到正或負(fù)匹配時(shí)停止計(jì)算,因此您應(yīng)在低優(yōu)先級(jí)路徑之前指定高優(yōu)先級(jí)路徑蜻直。 請(qǐng)注意盯质,只有URL的路徑組件用于比較。 忽略其他組件概而,例如查詢字符串或片段標(biāo)識(shí)符呼巷。
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)站路徑。 例如赎瑰,您可以:
- 使用*指定整個(gè)網(wǎng)站
- 包括特定URL,例如/ wwdc / news /,以指定特定鏈接
- 將*附加到特定網(wǎng)址,例如/ videos / wwdc / 2015 / ,以指定您網(wǎng)站的某個(gè)部分王悍。除了使用匹配任何子字符串,還可以使用餐曼? 匹配任何單個(gè)字符压储。 您可以在單個(gè)路徑中組合這兩個(gè)通配符鲜漩,例如/ foo / * / bar / 201?/ mypage
NOTE
The strings you use to specify website paths in the paths array are case sensitive.
用于在paths數(shù)組中指定網(wǎng)站路徑的字符串對(duì)大小寫是敏感的集惋。//即:區(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子目錄刮刑。 該文件需要通過(guò)HTTPS訪問(wèn)(無(wú)需任何重定向)喉祭,網(wǎng)址為https:// <domain> / apple-app-site-association或https:// <domain> /.well-known/apple-app-site-association 。 接下來(lái)雷绢,您需要處理應(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)用程序之間切換的相同機(jī)制,第二種是共享Web憑據(jù)(有關(guān)這些技術(shù)的更多信息翘紊,請(qǐng)參閱Web瀏覽器到本機(jī)應(yīng)用程序切換和共享 Web憑據(jù)參考)蔽氨。 當(dāng)用戶點(diǎn)擊universal link時(shí),iOS會(huì)啟動(dòng)您的應(yīng)用程序帆疟,并向其發(fā)送一個(gè)NSUserActivity對(duì)象鹉究,您可以查詢?cè)搶?duì)象以了解您的應(yīng)用程序是如何啟動(dò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鸯匹,請(qǐng)執(zhí)行以下步驟:
- 添加指定您的應(yīng)用支持的域的權(quán)利坊饶。
- 更新應(yīng)用程序委派以在接收到NSUserActivity對(duì)象時(shí)進(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í)行此操作,請(qǐng)打開(kāi)“ Capabilities”選項(xiàng)卡中的“Associated Domains”部分染厅,并為應(yīng)用程序支持的每個(gè)域添加一個(gè)條目痘绎,并以applinks:為前綴,如applinks:www.mywebsite.com肖粮。 將此列表限制為不超過(guò)大約20到30個(gè)域孤页。
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的所有子域,您可以通過(guò)使用前綴指定通配符涩馆。 在特定域的開(kāi)始之前(該期間是必需的)行施。 域匹配基于applinks條目中的最長(zhǎng)子字符串。 例如魂那,如果指定條目applinks:蛾号。mywebsite.com和applinks:。users.mywebsite.com涯雅,則對(duì)域emily.users.mywebsite.com的匹配將針對(duì)較長(zhǎng)的 .users.mywebsite.com條目執(zhí)行 鲜结。 請(qǐng)注意,* .mywebsite.com的條目與mywebsite.com不匹配,因?yàn)樾翘?hào)后的時(shí)間段精刷。 要為* .mywebsite.com和mywebsite.com啟用匹配拗胜,您需要為每個(gè)都提供單獨(dú)的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在用戶點(diǎn)擊universal link后啟動(dòng)應(yīng)用程序時(shí)纫事,您將收到一個(gè)activityType值為NSUserActivityTypeBrowsingWeb的NSUserActivity對(duì)象仰美。 活動(dòng)對(duì)象的webpageURL屬性包含用戶正在訪問(wèn)的網(wǎng)址。 這個(gè)webpage URL屬性始終包含HTTP或HTTPS URL儿礼,您可以使用NSURLComponents API來(lái)處理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)用戶點(diǎn)按您處理的universal link時(shí)庆寺,iOS還會(huì)檢查用戶最近的選擇蚊夫,以確定是打開(kāi)您的應(yīng)用還是您的網(wǎng)站。 例如懦尝,點(diǎn)擊universal link以打開(kāi)應(yīng)用的用戶可以稍后通過(guò)點(diǎn)擊狀態(tài)欄中的導(dǎo)航欄按鈕在Safari中選擇打開(kāi)您的網(wǎng)站知纷。 在用戶做出此選擇之后,iOS會(huì)繼續(xù)在Safari中打開(kāi)您的網(wǎng)站陵霉,直到用戶通過(guò)在網(wǎng)頁(yè)上的智能應(yīng)用橫幅中點(diǎn)擊打開(kāi)來(lái)選擇打開(kāi)您的應(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.
如果您實(shí)例化SFSafariViewController,WKWebView或UIWebView對(duì)象來(lái)處理通用鏈接踊挠,iOS會(huì)在Safari中打開(kāi)您的網(wǎng)站乍桂,而不是打開(kāi)您的應(yīng)用程序。 但是效床,如果用戶從嵌入式SFSafariViewController睹酌,WKWebView或UIWebView對(duì)象中輕擊通用鏈接,iOS會(huì)打開(kāi)您的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.
請(qǐng)務(wù)必了解憋沿,如果您的應(yīng)用使用openURL:打開(kāi)指向您網(wǎng)站的通用鏈接,該鏈接將始終在Safari中打開(kāi)沪猴,而不是重定向到您的應(yīng)用辐啄。 在這種情況下,iOS會(huì)識(shí)別呼叫來(lái)自您的應(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.
如果您在活動(dòng)對(duì)象中收到無(wú)效的網(wǎng)址,請(qǐng)務(wù)必妥善處理失敗洗出。 要處理不受支持的URL士复,可以在共享應(yīng)用程序?qū)ο笊险{(diào)用openURL:以在Safari中打開(kāi)鏈接。 如果您無(wú)法進(jìn)行此調(diào)用,請(qǐng)向用戶顯示一條錯(cuò)誤消息阱洪,說(shuō)明發(fā)生了什么問(wèn)題便贵。
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.
為了保護(hù)用戶的隱私和安全,當(dāng)您需要傳輸數(shù)據(jù)時(shí)冗荸,不應(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