iOS Universal Link 开发
来源:互联网 发布:吸尘器 卧式 桶式 知乎 编辑:程序博客网 时间:2024/06/06 10:42
背景
在iOS 9以前,我们从外部启动App都是通过URL Scheme实现跳转的。这种方式虽然可自定程度很高,能够巧妙地实现很多跳转,但弊端也很明显:我们只能通过scheme://example这种格式的链接来实现跳转,而且现在苹果还对这种方式的跳转加了一个提示框:“是否打开XXX”。对于对Web和原生App交互的场景需求量很大的产品来说,这样的跳转方式显然是步骤冗杂的,用户体验并不好。
iOS 9以后,Universal Link的出现解决了这个问题。它所提供的直接、顺畅、无缝衔接的跳转能够让用户体验提升一个级别。用户可以点击开发者指定的类似于https://example.com/t
的URL直接唤醒App,而不需要在浏览器打开再点击其他按钮。
在你的App中添加这个功能很简单:
在苹果开发者网站中打开需要使用Universal Link功能的App中的Associated Domains
上传apple-app-site-association到服务器根目录下
在AppDelegate中实现相应的方法
配置
首先,我们要在苹果开发者网站中开启App的Associated Domains功能。
在Account -> Certificates, Identifiers & Profiles -> App IDs -> YourApp -> Edit中把Associated Domains设置为Enable
然后我们需要配置一下工程文件,找到Capabilities -> Associated Domains
打开此功能并把你需要跳转的domain加进去,格式为applinks:www.example.com
部署
注意
首先你的服务器必须得支持SSL
接下来,我们需要上传一个json文件到我们的服务器。json文件以apple-app-site-association命名
注意
文件不需要添加任何后缀
json的格式是这样的:
{ "applinks": { "apps": [], "details": [ { "appID": "TeamID.com.domain.App", "paths":[ "*" ] } ] }}
appID
:TeamID加上Bundle IDpaths
:支持Universal Link,也就是可以跳转的路径。*
代表此域名下所有路径都支持,也可以具体制定到某个页面例如/path/page
或者某个路径下所有URL例如/path/*
关于paths
的配置,可以参考苹果官方文档
Use * to specify your entire website
Include a specific URL, such as /wwdc/news/, to specify a particular link
Append to a specific URL, such as /videos/wwdc/2015/, to specify a section of your website
In addition to using to match any substring, you can also use ? to match any single character. You can combine both wildcards in a single path, such as /foo//bar/201?/mypage.
最后,我们只需要把配置好的json文件上传到服务器中该域名的根目录下,言下之意,我们可以用GET请求可以获取到https://www.example.com/apple-app-association
再次强调必须是HTTPS协议
当我们的App在设备上第一次运行时,如果支持Associated Domains功能,那么iOS会自动去GET定义的Domain下的apple-app-site-association
文件。
需要留意iOS会先请求https://domain.com/.well-known/apple-app-site-association
如果此文件请求不到,再去请求https://domain.com/apple-app-site-association
所以如果想要避免服务器接收过多GET请求,可以直接把apple-app-site-association
放在./well-known/
目录下
注意
服务器上
apple-app-site-association
的更新不会让iOS本地的apple-app-site-association
同步更新,即iOS只会在App第一次启动时请求一次,以后除非App更新或重新安装否则不会在每次打开时请求apple-app-site-association
开发
待我们服务器部署好了,用curl测试一下apple-app-site-association
能够正确GET到了,那么我们就需要在工程中响应跳转事件了。
我们在AppDelegate中实现如下代理方法:
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{
具体实现:
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{ if (![userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) { return YES; } //读取url地址 NSURL *webUrl = userActivity.webpageURL; if (![webUrl.path isEqualToString:@"/show"]) { //path错误,直接从safari打开 [[UIApplication sharedApplication] openURL:webUrl]; return YES; } //跳转并显示内容 [[NSNotificationCenter defaultCenter] postNotificationName:@"notify" object:@"hello world"]; return YES;}
这里的自由度就很高了,我们可以根据传入的任何符合跳转条件的URL进行不同的操作。
测试
现在一切都已经完成了,现在我们可以在短信中点击一个URL直接跳转到我们的App。至于如何检验URL是否能够跳转,一个快捷方便的方法就是在系统原生App中(如短信、邮件等)长按URL,如果弹出的选项中有在“your app”中打开,那么证明该URL是支持跳转的。
注意
非系统原生App不一定能支持直接点击URL跳转,例如在微信中点击URL会首先在微信内的WebView打开,如果要跳转只能再通过Safari打开。
- iOS Universal Link 开发
- iOS Universal Link
- iOS 9下Universal Link(通用链接)开发
- Universal Link(iOS)踩坑
- ios通用链接(Universal Link)的处理
- universal link
- Universal Link|iOS开发者不得不知的技术
- 用Universal Link来实现ios的跳转
- Universal Link 通用链接
- Link - Windows Universal App - AppBar
- LINK - Windows Universal - 消息推送
- Universal Link 前端部署采坑记
- Link - Windows Universal App - Email/SMS/Share
- LINK - Windows Universal App - background media
- LINK - Windows Universal App in-app purchase
- iOS9上的Universal Link实现
- iOS: 创建universal app
- iOS-Universal-Framework
- HTTP协议历史演进的详细介绍
- Linux升级python3.x安装pip
- Struts1的logic:iterate标签
- vim编辑器 批量注释与反注释
- 多个Node.js版本之间切换
- iOS Universal Link 开发
- hdu2043
- css 初始化
- 书单
- linux命令
- 凸多边形
- hdu1753
- 贝叶斯定理在拼写检查中的应用
- MySQL登录出现ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)