87 字
1 分钟
openURL
2021-11-13
2026-10-12
无标签

前言#

在 UIKit 中我们通常使用下面方式来打开 外部链接,那么在 SwiftUI 中我们该怎样使用呢?

UIApplication.shared.open(url, options: [ : ], completionHandler: nil)

一、 注入环境变量#

@Environment(\.openURL) var openURL
...
ZStack {
Color.blue
Text("App Store")
.fontWeight(.medium)
.foregroundColor(.white)
.onTapGesture {
openURL.callAsFunction(url) { accepted in }
}
}
...
Link(destination: url, label: {
ZStack {
Color.blue
Text("App Store")
.fontWeight(.medium)
.foregroundColor(.white)
}
})

参考#

EnvironmentValues