You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI .onOpenURL 回调异常:通用链接模拟器正常真机无法触发

常见触发原因

  • iOS 14.6~14.8 系统级 SwiftUI 缺陷:该版本区间的物理设备存在已知的onOpenURL回调丢失问题,冷启动场景下点击 Universal Link 唤起App时不会触发该回调。模拟器的系统框架实现和物理机存在差异,因此不会复现该问题。
  • UIKit 生命周期拦截:如果项目同时保留了 SceneDelegate 相关代码,Universal Link 事件会优先被scene(_:continue:)生命周期方法捕获,若该方法未主动将URL透传到SwiftUI层,就会导致onOpenURL无回调。
  • 关联域名配置异常:物理机的网络环境如果无法正常拉取域名下的apple-app-site-association配置文件,可能出现仅触发App唤起、但未匹配到完整路径参数的情况,进而不会传递URL到SwiftUI层。

修复方案

优先添加UIKit生命周期兜底适配,兼容所有系统版本:

  1. 在项目的 SceneDelegate 中添加如下代码,主动捕获 Universal Link 回调:
// 处理热启动时的 Universal Link 事件
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
          let url = userActivity.webpageURL else { return }
    // 通过通知、全局状态或者环境变量将URL传递到SwiftUI层
    NotificationCenter.default.post(name: NSNotification.Name("UniversalLinkReceived"), object: url)
}

// 处理冷启动时的 Universal Link 事件
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    if let userActivity = connectionOptions.userActivities.first(where: { $0.activityType == NSUserActivityTypeBrowsingWeb }),
       let url = userActivity.webpageURL {
        NotificationCenter.default.post(name: NSNotification.Name("UniversalLinkReceived"), object: url)
    }
}
  1. 在 SwiftUI 层同时监听onOpenURL和通知回调,做双重兼容:
WindowGroup {
    ContentView()
        .onOpenURL { url in
            viewModel.urlFromOnOpenUrl = url
        }
        .onReceive(NotificationCenter.default.publisher(for: NSNotification.Name("UniversalLinkReceived"))) { output in
            guard let url = output.object as? URL else { return }
            viewModel.urlFromOnOpenUrl = url
        }
}

内容的提问来源于stack exchange,提问作者itopstory

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 05:06:03