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

iOS 26中Widget用widgetAccentedRenderingMode时Deep Link无法传递至App

问题说明

在iOS 16中,当SwiftUI Widget内的Link包含使用.widgetAccentedRenderingMode(除.fullColor外的任意模式)修饰的Image时,点击Widget中的图片可以正常启动App,但预期的Deep Link URL无法传递到SceneDelegate中。

复现步骤

  • 创建包含Link的SwiftUI Widget视图:
struct ImageView: View {
    var image: UIImage

    var body: some View {
        Link(destination: URL(string: "myapp://image")!) {
            Image(uiImage: image)
                .resizable()
                .widgetAccentedRenderingMode(.accentedDesaturated) // 注释此行后Deep Link可正常工作
                .scaledToFill()
                .clipped()
        }
    }
}
  • 在Info.plist中添加自定义URL Scheme:
<dict>
    <key>CFBundleURLName</key>
    <string>com.company.myapp</string>
    <key>CFBundleURLSchemes</key>
    <array>
        <string>myapp</string>
    </array>
</dict>
  • 在SceneDelegate中实现Deep Link处理逻辑:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    if let url = connectionOptions.urlContexts.first?.url {
        handle(url)
    }
}

func scene(_ scene: UIScene, openURLContexts urlContexts: Set<UIOpenURLContext>) {
    if let url = urlContexts.first?.url {
        handle(url)
    }
}

private func handle(_ url: URL) {
    // 在此处理URL
}
  • 在设备或模拟器上运行应用
  • 将Widget添加至主屏幕
  • 点击Widget内的图片

预期结果

应用启动后,SceneDelegate能接收到预期的Deep Link URL。

实际结果

应用正常启动,但SceneDelegate未接收到对应的Deep Link URL。

临时解决方案

自定义AccentedLink视图,通过在带渲染模式修饰的内容上叠加透明的Link来绕过问题:

struct AccentedLink<Content: View>: View {
    let destination: URL
    var content: Content

    var body: some View {
        content.overlay {
            Link(destination: destination) {
                Rectangle()
                    .fill(.clear)
            }
        }
    }

    init(destination: URL, @ViewBuilder content: () -> Content) {
        self.destination = destination
        self.content = content()
    }
}

使用AccentedLink替代原有的Link:

AccentedLink(destination: URL(string: "myapp://image")!) {
    Image(uiImage: image)
        .resizable()
        .widgetAccentedRenderingMode(.accentedDesaturated)
        .scaledToFill()
        .clipped()
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 14:14:56