一、先搞懂:Universal Link 是啥?
二、配置前的準備(必須先搞定)
三、分步配置(核心步驟,一步都不能少)
步驟 1:在蘋果開發者後台配置 App ID
步驟 2:在 Xcode 中配置關聯域名
步驟 3:創建 apple-app-site-association 文件(關鍵)
json
{
"applinks": {
"apps": [],
"details": [
{
"appID": "TeamID.BundleID", // 替換成你的團隊ID+App Bundle ID
"paths": [ "/app/*", "/" ] // 匹配的路徑,*是通配符,比如/app/*表示https://xxx.com/app/xxx都能跳轉
}
]
}
}
- TeamID:蘋果開發者後台 “Membership” 裏能看到;
- BundleID:你的 App 的唯一標識(比如 com.xxx.xxx);
- paths:指定哪些路徑能觸發跳轉,
["*"]表示所有路徑都可以(測試用,正式環境建議精準匹配)。
- 方式 1(推薦):放在域名的根目錄(比如https://xxx.com/apple-app-site-association);
- 方式 2:放在
.well-known目錄下(https://xxx.com/.well-known/apple-app-site-association); - 要求:文件必須能通過 HTTPS 訪問,且MIME 類型為 application/json(服務器配置,比如 Nginx 添加:
add_header Content-Type application/json;)。
步驟 4:App 內處理跳轉邏輯
swift
// iOS 13+ SceneDelegate
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
let url = userActivity.webpageURL // 獲取跳轉的Universal Link地址
if let url = url {
// 解析url,跳轉到對應頁面
print("跳轉鏈接:\(url)")
// 示例:如果是https://xxx.com/app/detail,跳轉到詳情頁
if url.path.contains("/app/detail") {
// 執行跳轉邏輯
}
}
}
}
// 兼容iOS 12及以下 AppDelegate
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
let url = userActivity.webpageURL
// 同上,解析url並跳轉
}
return true
}