ios - 如何将 SFSafariViewController 与 SwiftUI 一起使用?

标签 ios swift safari swiftui

我正在尝试从 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/

相关文章:

iphone - 如何解决通过 iPhone 模拟器 5.1 将文本发布到 Facebook 的共享工具包问题

ios - UISwitch Bool 失败? Switch isOn 不是真的

ios - 如何像原始图像一样在 UIImageView 中显示图像?

ios - 通过 Google AdMob 插页式委托(delegate)​​方法传递 segue 数据

javascript - 在 window.onbeforeunload 中等待消息发送?

objective-c - UITableView setContentOffset但不滚动tableView吗?

ios - UITableViewCell 文本居中对齐

swift - 以编程方式设置约束的问题

html5 音频 - canplay 事件不会在 Safari(Mac 桌面)上触发

javascript - Safari Javascript 与关键字冲突的 JSON 项目