通用鏈接為你提供了一些自定義 URL schemes 無法獲得的關鍵優(yōu)勢:唯一性,安全,靈活,簡單,私有等等
apple-app-site-association 文件
介紹:universal link 的配置文件
格式: JSON,切記不帶后綴
位置:根目錄下,或者 .well-known 目錄下,并支持 HTTPS,便于 Apple 訪問到: https://<fully qualified domain>/.well-known/apple-app-site-association
例子:
{
"applinks": {
"details": [
{
"appIDs": [ "ABCDE12345.com.example.app", "ABCDE12345.com.example.app2" ],
"components": [
{
"#": "no_universal_links",
"exclude": true,
"comment": "Matches any URL whose fragment equals no_universal_links and instructs the system not to open it as a universal link"
},
{
"/": "/buy/*",
"comment": "Matches any URL whose path starts with /buy/"
},
{
"/": "/help/website/*",
"exclude": true,
"comment": "Matches any URL whose path starts with /help/website/ and instructs the system not to open it as a universal link"
},
{
"/": "/help/*",
"?": { "articleNumber": "????" },
"comment": "Matches any URL whose path starts with /help/ and which has a query item with name 'articleNumber' and a value of exactly 4 characters"
}
]
}
]
}
}
appIDs:<Application Identifier Prefix>.<Bundle Identifier>
我們先來看看一個 URI 組成:
hierarchical part
┌───────────────────┴─────────────────────┐
authority path
┌───────────────┴───────────────┐┌───┴────┐
abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1
└┬┘ └───────┬───────┘ └────┬────┘ └┬┘ └─────────┬─────────┘ └──┬──┘
scheme user information host port query fragment
components: 見舉例中 commont
-
/匹配 URL path -
#匹配 URL fragment -
?匹配 URL query items -
exclude例外此規(guī)則 -
comment注釋 -
*匹配域名下所有 path -
/aa/bb/特定的鏈接 -
/aa/bb/*特定鏈接下的子集
Handle Universal Links
Xcode 配置:打開項目--> 選擇當前 Target --> Signing & Caoabilities --> + Associated Domains --> 以 applinks 開頭+apple-app-site-association文件存儲的域名,例如 applinks:www.apple.com
代碼捕獲:
func application(_ application: NSApplication,
continue userActivity: NSUserActivity,
restorationHandler: @escaping ([NSUserActivityRestoring]) -> Void) -> Bool
{
// handle action
if userActivity.activityType == NSUserActivityTypeBrowsingWeb { // handle universal links
guard let incomingURL = userActivity.webpageURL,
let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true) else { return false }
guard let path = components.path else {
return false
}
print("path = \(path), comp = \(components)")
}
return true
}
注意事項
緩存問題:如果修改了 apple-app-site-association ,重新運行沒有生效,則建議卸載 App 重新安裝。
apple-app-site-association 文件擯棄了舊的 path 字段改為 components,更靈活好用
Apple 查找順序 apple-app-site-association 文件順序, 優(yōu)先 /.well-known 目錄下

參考
Supporting Universal Links in Your App
Xcode 文檔 Supporting Associated Domains in Your App