“What is Universal Links?”
Apple 推出通用鏈接:一種能夠方便的通過(guò)傳統(tǒng) HTTP 鏈接來(lái)啟動(dòng) APP, 使用相同的網(wǎng)址打開(kāi)網(wǎng)站和 APP。
通過(guò)唯一的網(wǎng)址, 不需要特別的schema就可以鏈接一個(gè)特定的視圖到APP 里面 。比如:在微信中使用了通用鏈接, 那么用戶(hù)在Safari、UIWebView或者 WKWebView點(diǎn)擊一個(gè)鏈接, iOS設(shè)備上的微信app怎會(huì)在微信里面自動(dòng)打開(kāi)這個(gè)頁(yè)面, 如果沒(méi)有安裝則在Safrai中打開(kāi)響應(yīng)鏈接。
NOTE: Universal links let iOS 9 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.
For users who are running versions of iOS earlier than 9.0, tapping a universal link to your website opens the link in Safari.
“How to support Universal Links?”
Step1:創(chuàng)建一個(gè)json 格式的apple-app-site-associatio 文件如下:
{
"applinks": {
"apps": [],
"details": [
{
"appID": "9JA89QQLNQ.com.apple.wwdc",
"paths": [ "/wwdc/news/", "/videos/wwdc/2015/*" ]
},
{
"appID": "TeamID.BundleID2",
"paths": [ "*" ]
}
]
}
}
根據(jù) paths 鍵設(shè)定允許的路徑列表, 或只是一個(gè)星號(hào)如果你想打開(kāi) APP 而不管路徑是 什么
注意:paths 路徑是大小寫(xiě)敏感的
NOTE:The website paths you specify in the paths array are case sensitive.”
“appID”組成部分:TeamID + BundleId TeamID可以從蘋(píng)果開(kāi)發(fā)賬號(hào)頁(yè)面也“Your Account”下查看,BundleId就直接在工程里看了
Step2:上傳 apple-app-site-association 文件
注意:
1、上傳到web server根目錄下
2、web server 需要支持https,客戶(hù)端需要通告https訪(fǎng)問(wèn),并且不支持任何重定向
upload it to the root of your HTTPS web server. The file needs to be accessible via HTTPS—without any redirects—at https:///apple-app-site-association. Next, you need to handle universal links in your app.
Step3:在 APP 里處理通用鏈接
1、添加域名到 Capabilities
在 Xcode 的 capabilities 里 添加你的 APP 域名, 必須用 applinks: 前置它?

?這將使APP從上門(mén)的域名請(qǐng)求Step2中創(chuàng)建的JSON 文件 apple-app-site-association。當(dāng)你第一次啟動(dòng) APP,它會(huì)從 https://domain.com/apple-app-site-association 下載這個(gè)文件。
2、在 AppDelegate 里支持通用鏈接
實(shí)現(xiàn): - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *restorableObjects))restorationHandler方法,如下:

當(dāng) userActivity 是 NSUserActivityTypeBrowsingWeb 類(lèi)型, 則意味著它已經(jīng)由通用鏈接 API 代理。這樣的話(huà), 它保證用戶(hù)打開(kāi)的 URL 將有一個(gè)非空的 webpageURL 屬性
apple 官網(wǎng)地址:通用鏈接 Universal Links
@本文原博客地址: