我正在尝试从 NavigationButton
中呈现一个 SFSafariViewController
,但我不确定如何使用 SwiftUI 来实现。
在 UIKit 中,我会这样做:
let vc = SFSafariViewController(url: URL(string: "https://google.com"), entersReaderIfAvailable: true)
vc.delegate = self
present(vc, animated: true)
最佳答案
对 Matteo Pacini post 的补充, .presentation(Modal())
was removed by iOS 13's release .此代码应该有效(在 Xcode 11.3、iOS 13.0 - 13.3 中测试):
import SwiftUI
import SafariServices
struct ContentView: View {
// whether or not to show the Safari ViewController
@State var showSafari = false
// initial URL string
@State var urlString = "https://duckduckgo.com"
var body: some View {
Button(action: {
// update the URL if you'd like to
self.urlString = "https://duckduckgo.com"
// tell the app that we want to show the Safari VC
self.showSafari = true
}) {
Text("Present Safari")
}
// summon the Safari sheet
.sheet(isPresented: $showSafari) {
SafariView(url:URL(string: self.urlString)!)
}
}
}
struct SafariView: UIViewControllerRepresentable {
let url: URL
func makeUIViewController(context: UIViewControllerRepresentableContext<SafariView>) -> SFSafariViewController {
return SFSafariViewController(url: url)
}
func updateUIViewController(_ uiViewController: SFSafariViewController, context: UIViewControllerRepresentableContext<SafariView>) {
}
}
关于ios - 如何将 SFSafariViewController 与 SwiftUI 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56518029/