如何在 SwiftUI 应用程序内以椭圆形加载网页与在 safari 中加载
How to load a webpage in an ovally inside a SwiftUI App vs loading in safari
我想要创建的功能是当您单击其中一个警报时 ESPN 应用程序所做的...它加载应用程序但不是格式化视图,而是加载应用程序上的 Safari 视图可以被窃听(老实说,我讨厌那种情况,但在这样的情况下效果会很好。)
当前代码供参考
Button(action: {
openURL(URL(string: "linkhere")!)
}) {
Image("LISTENMENU")
}
我需要设置另一个视图并自己构建 webkitview 还是可以通过其他方式指定此功能? (也许通过修改 openURL 字符串
您需要包装 SFSafariViewController (which is from UIKit) into SwiftUI, since it isn't possible to do this natively right now. You should use UIViewControllerRepresentable 才能执行此操作。
import SwiftUI
import SafariServices
struct MyView: View {
@State var showWhosebug:Bool = false
var body: some View {
Button(action: { self.showWhosebug = true }) {
Text("Open Whosebug")
}
.sheet(isPresented: self.$showWhosebug) {
SFSafariViewWrapper(url: URL(string: "https://whosebug.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
}
}
我想要创建的功能是当您单击其中一个警报时 ESPN 应用程序所做的...它加载应用程序但不是格式化视图,而是加载应用程序上的 Safari 视图可以被窃听(老实说,我讨厌那种情况,但在这样的情况下效果会很好。)
当前代码供参考
Button(action: {
openURL(URL(string: "linkhere")!)
}) {
Image("LISTENMENU")
}
我需要设置另一个视图并自己构建 webkitview 还是可以通过其他方式指定此功能? (也许通过修改 openURL 字符串
您需要包装 SFSafariViewController (which is from UIKit) into SwiftUI, since it isn't possible to do this natively right now. You should use UIViewControllerRepresentable 才能执行此操作。
import SwiftUI
import SafariServices
struct MyView: View {
@State var showWhosebug:Bool = false
var body: some View {
Button(action: { self.showWhosebug = true }) {
Text("Open Whosebug")
}
.sheet(isPresented: self.$showWhosebug) {
SFSafariViewWrapper(url: URL(string: "https://whosebug.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
}
}