前言
When you support universal links, iOS 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.
https://developer.apple.com/library/archive/documentation/General/Conceptual/AppSearch/UniversalLinks.html
在wap中唤起app最广泛使用的方式并不是Universal Link,而是直接Schema跳转
代码语言:javascript复制location.href = 'schema://公众号:iOS逆向'
在 iOS9 之前,要在浏览器中唤醒 App,我们通常使用 scheme。这种方式需要提前判断系统中是否安装了能够响应此scheme的App,并且这种方式在微信被禁用。
代码语言:javascript复制- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation API_DEPRECATED_WITH_REPLACEMENT("application:openURL:options:", ios(4.2, 9.0)) API_UNAVAILABLE(tvos){
if ([[url absoluteString] hasPrefix:@"schema://"]) {
[[xxx sharedInstance] operationFromRouteURL:[url absoluteString]];//路由
return YES;
}
}
Universal Links 可以链接到您应用中的内容并安全地共享数据。
Universal Links 是标准 HTTP 或 HTTPS 链接,因此既适用于网站,也适用于应用程序。
- 如果未安装您的应用程序,则系统会在 Safari 中打开URL,以使您的网站能够处理它。浏览器可以正常跳转,
因此在没装App的时候,不会像schema出现网页无效的框.
- 当用户安装您的应用程序时,iOS 会检查存储在Web服务器上的文件,以验证您的网站是否允许您的应用程序代表其处理URL
在这里插入图片描述
I 、Adding support for universal links
- 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.
1.1 Creating and Uploading the Association File
代码语言:javascript复制https://developer.apple.com/library/archive/documentation/General/Conceptual/AppSearch/UniversalLinks.html
{
"applinks": {
"apps": [],
"details": [
{
"appID": "9JA89QQLNQ.com.apple.wwdc",
"paths": [ "/wwdc/news/", "/videos/wwdc/2015/*"]
},
{
"appID": "ABCD1234.com.apple.wwdc",
"paths": [ "*" ]
}
]
}
}
文件为json保存为文本即可 你的域名必须支持Https 域名根目录下放这个文件apple-app-association,不带任何后缀
官方测试地址:https://search.developer.apple.com/appsearch-validation-tool/
代码语言:javascript复制https://oia.zhihu.com/apple-app-association https://oia.zhihu.com/apple-app-site-association
{
"applinks": {
"apps": [
],
"details": {
"8J52SRPW6X.com.zhihu.ios": {
"paths": [
"*"
]
},
"886PYH8YW5.com.zhihu.ios": {
"paths": [
"*"
]
},
"B6MTNRMU2Y.com.zhihu.ios": {
"paths": [
"*"
]
},
"B6MTNRMU2Y.com.zhihu.ios-dev": {
"paths": [
"*"
]
}
}
},
"webcredentials": {
"apps": [
"8J52SRPW6X.com.zhihu.ios",
"886PYH8YW5.com.zhihu.ios",
"B6MTNRMU2Y.com.zhihu.ios",
"B6MTNRMU2Y.com.zhihu.ios-dev"
]
},
"activitycontinuation": {
"apps": [
"8J52SRPW6X.com.zhihu.ios",
"886PYH8YW5.com.zhihu.ios",
"B6MTNRMU2Y.com.zhihu.ios",
"B6MTNRMU2Y.com.zhihu.ios-dev"
]
}
}
1.2 Preparing Your App to Handle Universal Links
- 工程配置Associated Domains
- 编写App被唤醒后的处理逻辑
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler {
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
NSURL *webUrl = userActivity.webpageURL;
[self handleUniversalLink:webUrl]; // 转化为App路由
}
return YES;
}
代码语言:javascript复制- (void)handleUniversalLink:(NSURL*)webUrl{
NSString *host = webpageURL.host;
if ([host isEqualToString:@"apple..com"]) {
//进行我们需要的处理
}
else {
[[UIApplication sharedApplication]openURL:webpageURL];
}
}
II、注意事项
2.1 跨域
- iOS 9.2 之前,不用跨域都可以跳转, iOS 9.2 之后,必须跨域才能进行跳转到原生 App 上。
- iOS只会在 App 第一次启动时请求一次 apple-app-site-association 文件,服务器上该文件的更新不会让 iOS 本地的文件同步更新。
例子:为了解决苹果强制要求跨域才生效的问题,Universal Link就不能配置在主域名下,于是知乎才会准备一个oia.zhihu.com域名,专为Universal Link使用,
2.2 多业务线共用同一个 apple-app-association
多业务线共可以简单的把json file 进行merge, 共用同一个 apple-app-association
2.3 iOS拉取apple-app-association的时机
- 每次App安装后的第一次Launch,会拉取apple-app-association
- 除此之外在Appstore每次App的版本更新后的第一次Launch,也会拉取apple-app-association。
也就是说,让新的apple-app-association生效的方式是可以将App再发一个版本
III、iOS防止在WKWebView中打开Universal Link
Prevent universal links from opening in WKWebView
iOS防止在WKWebView中打开Universal Link
———————————————— 版权声明:本文为CSDN博主「#公众号:iOS逆向」的原创文章,遵循CC 4.0 BY-SA版权>协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/z929118967/article/details/119813220