这是我参与8月更文挑战的第18天,活动详情查看: 8月更文挑战” juejin.cn/post/698796… ”
前言
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.
在wap中唤起app最广泛使用的方式并不是Universal Link,而是直接Schema跳转
location.href = 'schema://公众号:iOS逆向'
复制代码
在 iOS9 之前,要在浏览器中唤醒 App,我们通常使用 scheme。这种方式需要提前判断系统中是否安装了能够响应此scheme的App,并且这种方式在微信被禁用。
- (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
{
"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,不带任何后缀
官方测试地址:search.developer.apple.com/appsearch-v…
{
"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"
]
}
}
复制代码
注意事项 :
- iOS 9.2 之前,不用跨域都可以跳转, iOS 9.2 之后,必须跨域才能进行跳转到原生 App 上。
- iOS只会在 App 第一次启动时请求一次 apple-app-site-association 文件,服务器上该文件的更新不会让 iOS 本地的文件同步更新。
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;
}
复制代码
- (void)handleUniversalLink:(NSURL*)webUrl{
NSString *host = webpageURL.host;
if ([host isEqualToString:@"apple..com"]) {
//进行我们需要的处理
}
else {
[[UIApplication sharedApplication]openURL:webpageURL];
}
}
复制代码
see also
公众号:iOS逆向
文章预告:iOS防止在WKWebView中打开Universal Link的方法:Prevent universal links from opening in WKWebView
————————————————
版权声明:本文为CSDN博主「#公众号:iOS逆向」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。