首页 > 解决方案 > 在 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 应用程序上全屏显示吗?谢谢

标签: iosswiftiphoneuikitswiftui

解决方案


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()
    }
}

推荐阅读