前言
在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
公众号:iOS逆向
代码语言: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,不带任何后缀
第一章节先来看看如何防止在WKWebView中打开Universal Link,如果你对如何获取Universal Link感兴趣可看第二章节iOS 创建 Universal Links
I 、防止在WKWebView中打开Universal Link
demo下载地址:https://download.csdn.net/download/u011018979/21361507
1.1 防止在WKWebView中打开Universal Link的原理:
防止在WKWebView中打开Universal Link的原理:在WKNavigationDelegate的协议方法- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
返回私有枚举_WKNavigationActionPolicyAllowWithoutTryingAppLink
static const WKNavigationActionPolicy WK_API_AVAILABLE(macosx(10.11), ios(9.0)) _WKNavigationActionPolicyAllowWithoutTryingAppLink = (WKNavigationActionPolicy)(WKNavigationActionPolicyAllow 2);
针对这个方案,很多网页采取的应对措施:提示用户使用系统浏览器访问
点击右上角选择浏览器打开
1.2 代码实现
代码语言:javascript复制/**
if you are using WKWebView, just use WKNavigationActionPolicyAllow 2 instead of WKNavigationActionPolicyAllow
*/
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
//返回 2的枚举值
decisionHandler(WKNavigationActionPolicyAllow 2);//_WKNavigationActionPolicyAllowWithoutTryingAppLink
}
II 、将应用和网站相关联以获取 Universal Links
注意事项 :iOS 9.2 之前,不用跨域都可以跳转, iOS 9.2 之后,必须跨域才能进行跳转到原生 App 上。
2.1 配置iOS App工程
公众号:iOS逆向
2.2 编写App被唤醒后的处理逻辑
代码语言:javascript复制- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler {
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {//当 userActivity 是 NSUserActivityTypeBrowsingWeb 类型, 则意味着它是由Universal Links 进来,也就是处理逻辑的时候。
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];
}
}
当 userActivity 是 NSUserActivityTypeBrowsingWeb 类型, 则意味着它是由Universal Links 进来,也就是处理逻辑的时候。
2.3 测试
官方测试地址: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"
]
}
}
更多关于Universal Links内容请看https://kunnan.blog.csdn.net/article/details/119773640
iOS 创建 Universal Links
III、h5唤起微信app支付
3.1 配置QueriesSchemes 白名单
按照微信文档配置以后,如果微信 Universal Links 部分没有问题,但是调不起来。那就是微信授权有问题,可在info.plist 配置QueriesSchemes。
在 LSApplicationQueriesSchemes 数组里分别添加 weixin、wechat、weixinULAPI 元素
公众号:iOS逆向
向微信开发者平台注册app的 bundle id 和 Universal Links
公众号:iOS逆向
see also
更多内容请查看原文:https://kunnan.blog.csdn.net/article/details/119813220
stackoverflow
https://stackoverflow.com/questions/38450586/prevent-universal-links-from-opening-in-wkwebview-uiwebview
WKNavigationActionPolicy
代码语言:javascript复制
/*! @enum WKNavigationActionPolicy
@abstract The policy to pass back to the decision handler from the
webView:decidePolicyForNavigationAction:decisionHandler: method.
@constant WKNavigationActionPolicyCancel Cancel the navigation.
@constant WKNavigationActionPolicyAllow Allow the navigation to continue.
*/
typedef NS_ENUM(NSInteger, WKNavigationActionPolicy) {
WKNavigationActionPolicyCancel,
WKNavigationActionPolicyAllow,
} API_AVAILABLE(macos(10.10), ios(8.0));
iOS 创建 Universal Links