Universal Link 學(xué)習(xí)記錄

universalLink

一、是什么

  • iOS 9之后推出的一個(gè)功能
  • 使APP可以通過(guò)傳統(tǒng)的HTTP請(qǐng)求來(lái)啟動(dòng)APP

二、為什么有

  • iOS 9 之前只能通過(guò)Scheme

三、怎么用

1、 必須有一個(gè)域名,且這個(gè)域名需要支持https

2、在開發(fā)者中心做配置:找到對(duì)應(yīng)的App ID,在Application Services列表里有Associated Domains一條,把它變?yōu)镋nabled

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、創(chuàng)建文件名為“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后綴
  • 如果要對(duì)沒(méi)有path的域名進(jìn)行支持(如:www.163.com),在json文件的paths中用通配符 '*'是不行的,需要在paths數(shù)組中加入’/’進(jìn)行匹配。
  • paths匹配的優(yōu)先級(jí)是從左至右依次降低
    • 比如"paths":[ "NOT /together/",""],在IOS9.2上path為/together/*都不會(huì)跳到App,但是在IOS9.0上會(huì)跳到。
  • 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 關(guān)聯(lián)起來(lái),這個(gè)關(guān)聯(lián)是雙向的

  • 域名對(duì) app 的認(rèn)證:在 web 服務(wù)器根目錄放一個(gè)名為 apple-app-association 的文本文件,里面描述哪些路徑會(huì)跳轉(zhuǎn)到哪個(gè) app。
    • 對(duì)被吊起APP的認(rèn)證
    • 對(duì)即將離開的APP的認(rèn)證
  • App 對(duì)域名的認(rèn)證:在 Xcode 里設(shè)置 Associated Domains,將域名添加進(jìn)去。
    • 哪個(gè)APP對(duì)哪個(gè)域名的認(rèn)證

6、上傳該文件到你的域名所對(duì)應(yīng)的根目錄或者.well-known目錄下

7、獲取用戶點(diǎn)擊進(jìn)入APP的鏈接,進(jìn)行相應(yīng)的頁(yè)面展示

    - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler

四、其他需要研究的方面

1、DeepLink

  • location.href = 'schema://xxxx'
    • 如果有安裝對(duì)應(yīng)APP則調(diào)起APP,開啟Associated Domains功能,那么iOS會(huì)自動(dòng)去獲取Domain下的apple-app-site-association文件
    • 如果沒(méi)有安裝對(duì)應(yīng)APP
      • safari會(huì)彈框打不開網(wǎng)頁(yè)
      • 延時(shí)之后再?gòu)棾鎏D(zhuǎn)APP STORE的彈框
  • normal url
    • 如果有安裝對(duì)應(yīng)APP則調(diào)起APP
    • 如果沒(méi)有安裝對(duì)應(yīng)APP則在瀏覽器中打開這個(gè)URL

2、封禁方案

* WKWebview
    * if you are using WKWebView, just use WKNavigationActionPolicyAllow + 2 instead of WKNavigationActionPolicyAllow

3、底層實(shí)現(xiàn)

  • +[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文件地址

五、注意點(diǎn)

1、只有當(dāng)前webview的url域名,與跳轉(zhuǎn)目標(biāo)url域名不一致時(shí),Universal Link 才生效。

  • 假如當(dāng)前網(wǎng)頁(yè)的域名是 A,當(dāng)前網(wǎng)頁(yè)發(fā)起跳轉(zhuǎn)的域名是 B ,必須要求 B 和 A 是不同域名,才會(huì)觸發(fā)Universal Link;如果B 和 A 是相同域名,只會(huì)繼續(xù)在當(dāng)前WebView里面進(jìn)行跳轉(zhuǎn),哪怕你的Universal Link一切正常,根本不會(huì)打開App
  • 從iOS 9.2開始,在相同的domain內(nèi)Universal Links是不work的,必須要跨域才生效

2、服務(wù)器上apple-app-site-association的更新不會(huì)讓iOS本地的apple-app-site-association同步更新

  • 只會(huì)在APP第一次啟動(dòng)時(shí)請(qǐng)求一次,除非APP更新或重新安裝否則不會(huì)在每次打開時(shí)請(qǐng)求apple-app-site-association。
  • 如果此時(shí)網(wǎng)絡(luò)連接出了問(wèn)題apple會(huì)緩存請(qǐng)求,等有網(wǎng)的時(shí)候再去請(qǐng)求,而實(shí)際測(cè)試抓包并沒(méi)有請(qǐng)求故通用連接會(huì)失效。

六、怎么工作的

  • 1.當(dāng)鏈接被點(diǎn)擊時(shí),iOS將會(huì)檢查該鏈接所關(guān)聯(lián)的域名是否注冊(cè)了 Universal Link,
  • 2.如果注冊(cè)了,再檢查響應(yīng)的應(yīng)用是否存在,如果存在,則打開該應(yīng)用;如果不存在,瀏覽器會(huì)加載http(s)鏈接

七參考資料

universal link in iOS

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容