iOS 26中Widget用widgetAccentedRenderingMode时Deep Link无法传递至App
iOS 16 Widget中带.widgetAccentedRenderingMode的Image无法传递Deep Link URL问题解决
问题说明
在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
相关产品推荐
相关产品推荐

