universalLink
一、是什么
- iOS 9之后推出的一个功能
- 使APP可以通过传统的HTTP请求来启动APP
二、为什么有
三、怎么用
1、 必须有一个域名,且这个域名需要支持https
2、在开发者中心做配置:找到对应的App ID,在Application Services列表里有Associated Domains一条,把它变为Enabled
3、打开工程配置中的Associated Domains,在其中的Domains中填入你想支持的域名,必须以applinks:为前缀
- The error “Add the associated Domains feature to your App ID” will go away once you enable the Associated Domains in your APP ID in developer.apple.com as in the previous step.
- If it doesn’t go away, quit and relaunch the xcode few times and it will work 😖😖😖.
4、创建文件名为“apple-app-site-association”的json格式的文件
- 是什么
- The AASA (short for apple-app-site-association) is a file that lives on your website and associates your website domain with your native app
- In other words, it’s a safe way to prove domain ownership to iOS.
- apple-app-site-association不需要.json后缀
- 如果要对没有path的域名进行支持(如:www.163.com),在json文件的paths中用通配符 '*'是不行的,需要在paths数组中加入’/’进行匹配。
- paths匹配的优先级是从左至右依次降低
- 比如"paths":[ "NOT /together/",""],在IOS9.2上path为/together/*都不会跳到App,但是在IOS9.0上会跳到。
- Supporting Multiple Apps on the Same Domain
- You can support multiple apps on the same domain.
- To do that, you’d need to add a new appID, path dictionary to the details array in the AASA file to look something like this:
- If two or more apps associate with the same content path on the website then the order of the appID, paths dictionary in the details array will determine which app will get precedence
5、将域名和 app 关联起来,这个关联是双向的
- 域名对 app 的认证:在 web 服务器根目录放一个名为 apple-app-association 的文本文件,里面描述哪些路径会跳转到哪个 app。
- App 对域名的认证:在 Xcode 里设置 Associated Domains,将域名添加进去。
6、上传该文件到你的域名所对应的根目录或者.well-known目录下
7、获取用户点击进入APP的链接,进行相应的页面展示
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
四、其他需要研究的方面
1、DeepLink
- location.href = 'schema://xxxx'
- 如果有安装对应APP则调起APP,开启Associated Domains功能,那么iOS会自动去获取Domain下的apple-app-site-association文件
- 如果没有安装对应APP
- safari会弹框打不开网页
- 延时之后再弹出跳转APP STORE的弹框
- normal url
- 如果有安装对应APP则调起APP
- 如果没有安装对应APP则在浏览器中打开这个URL
2、封禁方案
* WKWebview
* if you are using WKWebView, just use WKNavigationActionPolicyAllow + 2 instead of WKNavigationActionPolicyAllow
3、底层实现
- +[LSAppLink openWithURL:completionHandler:] this is how universal link open corresponding app. you can exchange its implementations with yourself method but be carefule,this is private API.
- LPLink文件地址
五、注意点
1、只有当前webview的url域名,与跳转目标url域名不一致时,Universal Link 才生效。
- 假如当前网页的域名是 A,当前网页发起跳转的域名是 B ,必须要求 B 和 A 是不同域名,才会触发Universal Link;如果B 和 A 是相同域名,只会继续在当前WebView里面进行跳转,哪怕你的Universal Link一切正常,根本不会打开App
- 从iOS 9.2开始,在相同的domain内Universal Links是不work的,必须要跨域才生效
2、服务器上apple-app-site-association的更新不会让iOS本地的apple-app-site-association同步更新
- 只会在APP第一次启动时请求一次,除非APP更新或重新安装否则不会在每次打开时请求apple-app-site-association。
- 如果此时网络连接出了问题apple会缓存请求,等有网的时候再去请求,而实际测试抓包并没有请求故通用连接会失效。
六、怎么工作的
- 1.当链接被点击时,iOS将会检查该链接所关联的域名是否注册了 Universal Link,
- 2.如果注册了,再检查响应的应用是否存在,如果存在,则打开该应用;如果不存在,浏览器会加载http(s)链接
七参考资料
universal link in iOS