PDFKit不能与文档选择器一起显示吗?

时间:2019-10-10 00:51:15

标签: ios swift uidocumentpickerviewcontroller ios-pdfkit apple-pdfkit

我试图使用PDFKit在应用程序中查看pdf,并使用DocumentPicker允许用户选择他们要查看的pdf。最终目标是从数据库检索pdf网址,这样文件就不会存储在本地。但是,使用DocumentPicker选择文件时,PDFView将为空。我在路径之间看到的唯一区别似乎是.tmp签名,并且不确定这是否阻止了文档的显示。 file:///private/var/mobile/Containers/Data/Application/[Device]/tmp/com.company.app-Inbox/Welcome.pdf位于Im读取或选择的路径上,这是应该在PDFView file:///var/mobile/Containers/Data/Application/[Device]/Documents/Welcome.pdf中显示的路径。我正在使用一个按钮进入文档选择器,然后触发对PFDView的选择。



    var urlPath: URL!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {

        let pdfView = PDFView(frame: self.view.bounds)
        self.view.addSubview(pdfView)

        let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let sandboxUrl = dir.appendingPathComponent(urlPath.lastPathComponent)

        if !FileManager.default.fileExists(atPath: sandboxUrl.path) {
            do {
                try FileManager.default.copyItem(at: urlPath, to: sandboxUrl)
            } catch {
                print("error copying")
            }
        } else {

        }
        print("here is the urlpath: \(urlPath!.absoluteString)")
        print("here is the pdfpath: \(sandboxUrl)")

        pdfView.translatesAutoresizingMaskIntoConstraints = false
//        view.addSubview(pdfView)

        pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
        pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
        pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
        pdfView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true

        guard let path = Bundle.main.url(forResource: sandboxUrl.absoluteString, withExtension: "pdf") else { return }


        if let document = PDFDocument(url: path) {
            pdfView.document = document
        }
    }
}```

0 个答案:

没有答案