ios - 在 SwiftUI 中使用 UIViewControllerRepresentable 呈现 UIDocumentInteractionController
问题描述
我正在尽可能使用 SwiftUI 创建一个新的 iOS 应用程序。但是,我希望能够生成包含一些数据的 PDF。在没有 swiftUI 的类似项目中,我可以做到这一点
let docController = UIDocumentInteractionController.init(url: "PATH_TO_FILE")
docController.delegate = self
self.dismiss(animated: false, completion: {
docController.presentPreview(animated: true)
})
只要在视图控制器的其他地方我有这个:
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self
}
我可以走了。我无法解决的是如何将其应用于 UIViewControllerRepresentable 并让它在 SwiftUI 中工作。我的 UIViewControllerRepresentable 是否应该以成为 UIViewController 为目标?然后如何设置委托和presentPreview?这会像我的标准 iOS 应用程序那样覆盖任何视图并在我的 SwiftUI 应用程序上全屏显示吗?谢谢
解决方案
UIDocumentInteractionController
这是从 SwiftUI 视图集成以供使用的可能方法。
全模块代码。使用 Xcode 11.2 / iOS 13.2 测试
import SwiftUI
import UIKit
struct DocumentPreview: UIViewControllerRepresentable {
private var isActive: Binding<Bool>
private let viewController = UIViewController()
private let docController: UIDocumentInteractionController
init(_ isActive: Binding<Bool>, url: URL) {
self.isActive = isActive
self.docController = UIDocumentInteractionController(url: url)
}
func makeUIViewController(context: UIViewControllerRepresentableContext<DocumentPreview>) -> UIViewController {
return viewController
}
func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<DocumentPreview>) {
if self.isActive.wrappedValue && docController.delegate == nil { // to not show twice
docController.delegate = context.coordinator
self.docController.presentPreview(animated: true)
}
}
func makeCoordinator() -> Coordintor {
return Coordintor(owner: self)
}
final class Coordintor: NSObject, UIDocumentInteractionControllerDelegate { // works as delegate
let owner: DocumentPreview
init(owner: DocumentPreview) {
self.owner = owner
}
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return owner.viewController
}
func documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController) {
controller.delegate = nil // done, so unlink self
owner.isActive.wrappedValue = false // notify external about done
}
}
}
// Demo of possible usage
struct DemoPDFPreview: View {
@State private var showPreview = false // state activating preview
var body: some View {
VStack {
Button("Show Preview") { self.showPreview = true }
.background(DocumentPreview($showPreview, // no matter where it is, because no content
url: Bundle.main.url(forResource: "example", withExtension: "pdf")!))
}
}
}
struct DemoPDFPreview_Previews: PreviewProvider {
static var previews: some View {
DemoPDFPreview()
}
}
推荐阅读
- javascript - 智利的 Node JS 当前 DST 规则
- database - 删除一个表中的一行会触发另一个表中的删除
- arrays - 包含索引的矩阵和列向量,如何在没有循环的情况下进行迭代?
- php - 如何获取 WooCommerce 中的活跃订阅者列表?
- eclipse - 如何在 SVN 中存储 Eclipse C 项目
- sql - 从旧数据库中获取表数据
- html - 使用在 server.R 上创建的变量列表填充 html 文本
- node.js - 通过 NodeJS Lambda 函数向 Slack 发布消息 - 数据变量未呈现
- azure - 用于具有 nexus 注册表的容器的 azure web 应用程序
- spring - Spring 并行处理 ZipFile 中的 ZipEntries