通用链接为你提供了一些自定义 URL schemes 无法获得的关键优势:唯一性,安全,灵活,简单,私有等等
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
例外此规则 -
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 文件顺序, 优先 /.well-known 目录下
参考
Supporting Universal Links in Your App
Xcode 文档 Supporting Associated Domains in Your App
网友评论