Deep Link 与 URL Scheme
问题
iOS 中 Deep Link 的实现方式有哪些?
答案
URL Scheme
Info.plist
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>myapp</string>
</array>
</dict>
</array>
// 处理打开
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else { return }
// myapp://user/profile?id=123
handleDeepLink(url)
}
Universal Links(推荐)
apple-app-site-association(服务端域名根目录)
{
"applinks": {
"apps": [],
"details": [{
"appID": "TEAMID.com.example.app",
"paths": ["/user/*", "/product/*"]
}]
}
}
// App 不需要注册 URL Scheme,系统自动识别
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let url = userActivity.webpageURL else { return }
// https://example.com/user/123
handleDeepLink(url)
}
对比
| URL Scheme | Universal Links | |
|---|---|---|
| 格式 | myapp://path | https://domain/path |
| 需要安装 App | 无法回退 | 可回退到网页 |
| 弹出确认框 | ❌ 无 | ❌ 无 |
| 安全性 | 低(任何 App 可注册) | 高(域名验证) |
常见面试问题
Q1: Universal Links 不生效怎么排查?
答案:
- 检查
apple-app-site-association文件是否可访问(无重定向,HTTPS) - 检查 Entitlements 中
Associated Domains格式applinks:example.com - 重新安装 App(首次安装时下载 AASA 文件)
- 长按链接不要选"在 Safari 中打开"(会触发屏蔽)