从外部链接打开App:deepLink与universalLink选择及配置咨询
选择Deep Link还是Universal Link?
核心结论
优先使用Universal Link,这是苹果官方推荐的方案,完全匹配你“点击外部链接直接打开App并读取URL”的需求,相比传统Deep Link,它无系统弹窗、安全性更高,还支持未安装App时跳转到网页。
两者核心差异
- Deep Link:基于自定义URL Scheme(如
myapp://open?param=123),实现简单,但安全性差(任意App可注册相同Scheme),且外部打开时会触发“是否允许打开App”的系统弹窗,体验不佳。 - Universal Link:基于标准HTTPS域名(如
https://yourdomain.com/open/123),苹果会验证域名与App的归属关系,点击链接直接打开App,无额外弹窗,体验流畅。
是否需要网站配置JSON文件?
是,Universal Link必须配置apple-app-site-association(无后缀JSON文件),用于苹果验证你的App与域名的关联权限。
操作步骤与真实示例
1. 苹果开发者后台配置
- 登录苹果开发者中心,找到目标App ID,开启Associated Domains功能。
- 添加关联域名,格式为
applinks:yourdomain.com(例如applinks:myapp.example.com)。
2. 网站端配置apple-app-site-association文件
创建无后缀的JSON文件,内容示例:
{ "applinks": { "details": [ { "appIDs": ["YOUR_TEAM_ID.com.yourcompany.yourapp"], "components": [ { "/": "/open/*", "comment": "匹配所有/open/开头的HTTPS链接" } ] } ] } }
- 替换
YOUR_TEAM_ID为你的苹果开发者团队ID,com.yourcompany.yourapp为App的Bundle ID。 components定义触发App的URL规则,示例中所有https://myapp.example.com/open/xxx格式的链接都会唤起App。- 将文件上传到域名根目录(如
https://myapp.example.com/apple-app-site-association)或.well-known子目录(https://myapp.example.com/.well-known/apple-app-site-association),确保文件可通过HTTPS直接访问,无重定向。
3. iOS App代码配置
UIKit 示例
在AppDelegate.swift中处理链接:
import UIKit @main class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // 处理冷启动时的链接 if let url = launchOptions?[.url] as? URL { handleIncomingURL(url: url) } return true } func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool { // 处理App已启动时的Universal Link if userActivity.activityType == NSUserActivityTypeBrowsingWeb, let url = userActivity.webpageURL { handleIncomingURL(url: url) } return true } private func handleIncomingURL(url: URL) { // 读取并处理传入的URL,示例打印URL内容 print("收到的链接: \(url.absoluteString)") // 可根据URL路径/参数跳转到对应页面 let targetVC = DetailViewController() targetVC.linkURL = url UIApplication.shared.windows.first?.rootViewController?.present(targetVC, animated: true) } }
SwiftUI 示例
在App.swift中处理链接:
import SwiftUI @main struct MyApp: App { var body: some Scene { WindowGroup { ContentView() .onOpenURL { incomingURL in // 处理传入的URL print("收到的链接: \(incomingURL.absoluteString)") // 可通过环境对象或通知将URL传递给对应页面 } } } }
4. 测试验证
- 将App安装到真机(模拟器无法完全模拟外部链接唤起场景)。
- 在iMessage中发送测试链接(如
https://myapp.example.com/open/test123),点击链接应直接打开App,控制台会打印出该URL。
苹果官方文档参考
苹果开发者文档:《Supporting Universal Links in Your App》,地址为 developer.apple.com/documentation/xcode/supporting-universal-links-in-your-app
内容的提问来源于stack exchange,提问作者martin wotterman
相关产品推荐
相关产品推荐

