iOS 创建 Universal Links
原创
©著作权归作者所有:来自51CTO博客作者#公众号:iOS逆向的原创作品,请联系作者获取转载授权,否则将追究法律责任
前言
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.
developer.apple.com/library/arc…
在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
developer.apple.com/library/arc…
{
"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…
oia.zhihu.com/apple-app-a…
oia.zhihu.com/apple-app-s…
{
"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
see also
公众号:iOS逆向