Universal Link 的奇幻旅程
Universal Link 是一個令開發者又愛又恨的東西,它可以增進使用者體驗,但出問題時卻很難 Debug
今天要分享我們遇到 Universal Link 出錯時,一步一步除錯並解決問題的過程,並分享一些簡單的 Debug 方式
什麼是 Universal Link?
iOS 的 Universal Link 是一種網頁連結的標準,可以讓開發者在網頁上放置特定格式的連結,並且在用戶點擊這些連結時,讓 iOS 裝置直接開啟對應的應用程式,而不是在瀏覽器中顯示該網頁。
- By ChatGPT
關於 Universal Link 的使用方式以及運作原理網路上已有許多教學文章,本篇文章就不多加以解釋
我們遇到什麼問題?
- 已經在
.well-known
資料夾底下放了apple-app-site-association
檔案,Xcode 也已經設置Associated Domain
- 點擊網址仍無法直接打開並跳轉到 App,只會開 Safari
- 設定
mode=developer
繞過 Apple CDN 後,卻可以正常打開 App
什麼是 Apple 的 CDN?
官方的技術文件裡面有這麼幾段話
Starting with macOS 11 and iOS 14, apps no longer send requests for
apple-app-site-association
files directly to your web server. Instead, they send these requests to an Apple-managed content delivery network (CDN) dedicated to associated domains.Apple’s content delivery network requests the
apple-app-site-association
file for your domain within 24 hours. Devices check for updates approximately once per week after app installation.
簡單來說:
- Apple 的 CDN 每 24 小時會去跟你的 Server 下載
apple-app-site-association
檔案 - 使用者的裝置會再跟 Apple 的 CDN 下載這個檔案
- 下載完後存在裝置上,大概 7 天會檢查一次更新
在 Associated Domain 的 URL 設定 mode=developer
則可以繞過 Apple CDN,直接從你的 Server 下載檔案。範例:
applinks:www.dcard.tw?mode=developer
思路
根據以上資訊,因為繞過 CDN 可以正常打開 App,我們可以確定的是:
apple-app-site-association
檔案設置正確Associated Domain
設置正確
因此得出以下推論:
- Apple CDN 沒抓到檔案
- 裝置沒透過 CDN 抓到檔案
嘗試 Reset 模擬器,重新安裝 App 後仍然無法跳轉,算是間接驗證了不是 裝置沒透過 CDN 抓到檔案 的問題,那麼可能性只剩下 Apple CDN 沒抓到檔案
Google 了一下後發現有一個網址可以檢測 Apple CDN 有沒有抓到你的 apple-app-site-association
檔案
https://app-site-association.cdn-apple.com/a/v1/your.domain.name
把網址輸入上去後,得到 Not Found 的結果
由於這個網址沒有正式的文件,抱著姑且一試的 Developer Console ,發現了有趣的東西
其中一個 Response Header 出現了 403 Forbidden 的訊息,我的直覺告訴我可能是 Apple CDN 沒辦法正常存取我們的網站,為了近一步驗證回傳的訊息是可靠的
身為一個專業的前端,亂戳看看 API 是一定要的,輸入一個不存在的網址,回傳了 404 Not Found & No Such Host
的訊息,間接證明了這個 API 回傳的 Header 是有規矩的,不是隨便亂傳,更加確信 Apple CDN 被我們的防火牆擋住了!
拿上圖出現的 SWCERR00101 Bad HTTP Response: 403 Forbidden
訊息去 Google 後,發現 Apple 官方人員確實解答了出現此錯誤訊息時,代表 Apple CDN 無法存取檔案
接下來問題就簡單啦!聯絡負責 Security 的同事,發現我們的防火牆的確把 Apple 的 Bot 擋住了!
Apple 的 Bot 的 User-Agent 是:AASA-Bot/1.0.0
在解開防火牆限制後大約 30 分鐘,Universal Link 就正常運作了!🎉
希望本篇文章可以幫助到你!
tl;dr,遇到 Universal Link 無法正常打開 App 時可以怎麼 Debug?
- 先檢查可不可以正常存取你 Server 上的
./well-known/apple-app-site-association
- 若可以存取,請將 Xcode 中的 Associated Domain 設置為
mode=developer
,再嘗試看看 (詳情請參閱官方文件)。若無法打開,代表可能apple-app-site-association
檔案或Associated Domain
設置有誤 - 若可以打開,代表可能 Apple CDN 抓不到
apple-app-site-association
檔案,請使用以下網址,輸入你自己的 Domain Name 來檢查:
https://app-site-association.cdn-apple.com/a/v1/your.domain.name
若運作正常,應該要出現你的 apple-app-site-association
檔案,範例:
若無法顯示檔案內容,請檢查是不是你的 Server 把 Apple 的 Bot 擋住了,導致 Apple 無法存取檔案。
Apple 的 Bot 的 User-Agent 是:AASA-Bot/1.0.0