1. 配置 App ID
正确操作:
-
登录 Apple Developer。
-
进入 Certificates, Identifiers & Profiles > Identifiers。
-
找到你的 App ID 或创建一个新的 App ID。
-
在 App ID 的详情页,启用 Associated Domains 功能。
- 如果没有启用此功能,Universal Link 无法使用。
2. 配置 Associated Domains
正确操作:
-
在 Apple Developer 网站,确保在 App ID 中添加了 Associated Domains 功能。
-
在 Xcode 中,确保已启用 Associated Domains:
- 打开项目 > Signing & Capabilities > 点击 + Capability。
- 添加 Associated Domains。
-
在 Associated Domains 中添加你的域名:
- 格式:
applinks:yourdomain.com。 - 如果有子域名:
applinks:sub.yourdomain.com。
- 格式:
3. 配置 apple-app-site-association 文件
正确操作:
- 在服务器根目录或
.well-known目录中创建apple-app-site-association文件。 - 文件内容如下:
json
复制代码
{
"applinks": {
"apps": [],
"details": [
{
"appID": "TEAMID.com.yourcompany.yourapp",
"paths": [ "/path1/*", "/path2/*", "NOT /excluded-path/*", "/" ]
}
]
}
}
-
TEAMID:开发者团队 ID,可在 Apple Developer 查看。
-
Bundle Identifier:应用的唯一标识符。
-
paths:
-
匹配规则:
"/":匹配所有路径。"/path/*":匹配特定路径下的内容。"NOT /path/*":排除某些路径。
-
-
将文件上传到以下位置:
- 推荐路径 1:
https://yourdomain.com/apple-app-site-association - 推荐路径 2:
https://yourdomain.com/.well-known/apple-app-site-association
- 推荐路径 1:
-
确保文件通过 HTTPS 提供,并使用有效的 SSL 证书。
4. 验证 apple-app-site-association 文件
正确操作:
-
在浏览器中访问:
https://yourdomain.com/apple-app-site-association- 或
https://yourdomain.com/.well-known/apple-app-site-association
-
验证返回的文件内容是否符合 JSON 格式,且
Content-Type设置为:application/json或application/pkcs7-mime(推荐)。
5. 在应用中处理 Universal Link
正确操作:
在应用中通过以下代码处理 Universal Link:
swift
复制代码
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let url = userActivity.webpageURL else {
return false
}
// 根据 URL 处理逻辑
handleUniversalLink(url)
return true
}
func handleUniversalLink(_ url: URL) {
print("Received Universal Link: (url)")
// 例如:解析 URL 路径或参数
if url.path == "/specific-path" {
// 执行特定逻辑
}
}
6. 测试 Universal Link
正确操作:
-
安装应用:
- 使用 Xcode 将应用安装到真实设备上(模拟器可能无法正常测试)。
-
准备测试链接:
- 创建一个 Universal Link,例如
https://yourdomain.com/specific-path。
- 创建一个 Universal Link,例如
-
测试行为:
- 在 Safari、iMessage、邮件中点击链接。
- 检查是否正确跳转到应用。
-
监控效果:
- 如果 Universal Link 未生效,可能是
apple-app-site-association文件配置或 Associated Domains 配置的问题。
- 如果 Universal Link 未生效,可能是
常见问题与解决方法
问题 1:链接无法打开应用
- 确保已启用 Associated Domains 功能。
- 确认
apple-app-site-association文件正确配置且路径无误。 - 使用 HTTPS 提供服务,并确保证书有效。
问题 2:文件无法加载
- 检查服务器是否返回正确的 MIME 类型。
- 使用工具验证文件是否符合 Apple 的要求,例如 AASA Validator。
问题 3:跳转失败或打开浏览器
- 检查链接的路径是否在
paths中正确定义。 - 确保设备上已安装正确版本的应用。