gpt4 book ai didi

ios - 在 SwiftUI 中使用 UIViewControllerRepresentable 呈现 UIDocumentInteractionController

转载 作者:行者123 更新时间:2023-12-01 18:35:19 35 4
gpt4 key购买 nike

我正在尽可能使用 SwiftUI 创建一个新的 iOS 应用程序。但是,我希望能够生成包含一些数据的 PDF。
在没有 swiftUI 的类似项目中,我可以做到这一点

let docController = UIDocumentInteractionController.init(url: "PATH_TO_FILE")
docController.delegate = self
self.dismiss(animated: false, completion: {
docController.presentPreview(animated: true)
})

只要在 View Controller 的其他地方我有这个:
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self
}

我可以走了。
我无法解决的是如何将其应用于 UIViewControllerRepresentable 并让它在 SwiftUI 中工作。我的 UIViewControllerRepresentable 是否应该以成为 UIViewController 为目标?然后如何设置委托(delegate)和presentPreview?这会像我的标准 iOS 应用程序那样覆盖任何 View 并在我的 SwiftUI 应用程序上全屏显示吗?
谢谢

最佳答案

这是集成 UIDocumentInteractionController 的可能方法从 SwiftUI View 中使用。

demo

全模块代码。使用 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()
}
}

关于ios - 在 SwiftUI 中使用 UIViewControllerRepresentable 呈现 UIDocumentInteractionController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60354684/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com