问题描述
我要创建的功能是单击警报之一时ESPN应用程序所执行的操作...它加载该应用程序,但不格式化视图,而是将其加载到可点击的Safari视图上离开(老实说,我讨厌那样,但是在这样的情况下,效果很好。)
当前参考代码
Button(action: {
openURL(URL(string: "linkhere")!)
}) {
Image("LISTENMENU")
}
我需要自己设置另一个视图并自己构建webkitview还是可以用其他方式指定此功能? (也许通过修改openURL字符串
解决方法
您需要将SFSafariViewController(来自UIKit)包装到SwiftUI中,因为目前无法在本地进行此操作。您应该使用UIViewControllerRepresentable来完成此操作。
import SwiftUI
import SafariServices
struct MyView: View {
@State var showStackoverflow:Bool = false
var body: some View {
Button(action: { self.showStackoverflow = true }) {
Text("Open stackoverflow")
}
.sheet(isPresented: self.$showStackoverflow) {
SFSafariViewWrapper(url: URL(string: "https://stackoverflow.com")!)
}
}
}
struct SFSafariViewWrapper: UIViewControllerRepresentable {
let url: URL
func makeUIViewController(context: UIViewControllerRepresentableContext<Self>) -> SFSafariViewController {
return SFSafariViewController(url: url)
}
func updateUIViewController(_ uiViewController: SFSafariViewController,context: UIViewControllerRepresentableContext<SFSafariViewWrapper>) {
return
}
}