Universal Link在关联App内调用时跳转至Safari而非App的问题咨询
解决Universal Link内部跳转Safari的问题
核心修改方案
在调用open(_:options:completionHandler:)时,必须在options参数中添加[.universalLinksOnly: true],强制系统使用Universal Link逻辑处理该URL,而非默认打开Safari。
修改后的代码:
let url = Foundation.URL(string: "https://workingdeeplink.com/")! if UIApplication.shared.canOpenURL(url) { UIApplication.shared.open(url, options: [.universalLinksOnly: true], completionHandler: nil) }
原因解析
iOS系统对App内部打开自身关联的Universal Link有特殊处理:默认会把这类URL当作普通HTTP链接,直接跳转Safari,而不会触发App内的Universal Link路由逻辑。通过指定universalLinksOnly选项,明确告诉系统必须用Universal Link规则处理该链接,这样就能在App内完成跳转。
额外注意
如果需要处理链接的具体页面路由,确保已经在App中实现了对应的处理逻辑:
- UIKit项目:实现
AppDelegate的application(_:continue:restorationHandler:)方法 - SwiftUI项目:在
onOpenURL修饰器中处理URL路由
内容的提问来源于stack exchange,提问作者Thomas
相关产品推荐
相关产品推荐

