迅速查看pdf 4

时间:2019-07-07 08:32:38

标签: swift4

我已经尝试了互联网上的所有内容,以便在我的应用程序中添加PDFViewer。我与ios 12一起工作。我要您帮助我了解添加pdf的可能方法是什么,以及对我缺乏快速编码经验的解决方案,可以轻松地解决该问题。谢谢

3 个答案:

答案 0 :(得分:1)

我们可以使用我们的本机UIDocumentInteractionController。

请按照以下步骤操作:

第1步

 var documentInteractionController = UIDocumentInteractionController()

第2步

 self.documentInteractionController.delegate = self

第3步

  func openDocument(atURL url: URL, screenTitle: String) {
    self.documentInteractionController.url = url
    self.documentInteractionController.name = screenTitle
    self.documentInteractionController.delegate = self
    self.documentInteractionController.presentPreview(animated: true)
}

第4步:实现UIDocumentInteractionControllerDelegate

extension ViewController: UIDocumentInteractionControllerDelegate  {
// when a document interaction controller needs a view controller for presenting a document preview.

func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
    return self.navigationController ?? UIViewController()
}

}

一些辅助方法:

a)查看Pdf

  func viewPdf(urlPath: String, screenTitle: String) {
    // open pdf for booking id
    guard let url = urlPath.toUrl else {
        print("Please pass valid url")
        return
    }

    self.downloadPdf(fileURL: url, screenTitle: screenTitle) { localPdf in
        if let url = localPdf {
            DispatchQueue.main.sync {
                self.openDocument(atURL: url, screenTitle: screenTitle)
            }
        }
    }
}

b)下载文件功能

  // method  for download pdf file
func downloadPdf(fileURL: URL, screenTitle: String, complition: @escaping ((URL?) -> Void)) {
    // Create destination URL
    if let documentsUrl: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
        let destinationFileUrl = documentsUrl.appendingPathComponent("\(screenTitle).pdf")

        if FileManager.default.fileExists(atPath: destinationFileUrl.path) {
            try? FileManager.default.removeItem(at: destinationFileUrl)
        }

        let sessionConfig = URLSessionConfiguration.default
        let session = URLSession(configuration: sessionConfig)

        let request = URLRequest(url: fileURL)

        let task = session.downloadTask(with: request) { tempLocalUrl, response, error in
            if let tempLocalUrl = tempLocalUrl, error == nil {
                // Success
                if let statusCode = (response as? HTTPURLResponse)?.statusCode {
                    print("Successfully downloaded. Status code: \(statusCode)")
                }

                do {
                    try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
                    complition(destinationFileUrl)
                } catch let writeError {
                    print("Error creating a file \(destinationFileUrl) : \(writeError)")
                }

            } else {
                print("Error took place while downloading a file. Error description: \(error?.localizedDescription ?? "N/A")")
            }
        }
        task.resume()
    } else {
        complition(nil)
    }
}

答案 1 :(得分:0)

在这里,我正在下载PDF并存储在文件中,然后在“快速查看”中打开该文件

我在这里共享屏幕 enter image description here

参考链接:https://www.hackingwithswift.com/example-code/libraries/how-to-preview-files-using-quick-look-and-qlpreviewcontroller

答案 2 :(得分:0)

如果只需要显示PDF,则可以使用WebKit中的WebView并使用mimetype application / pdf传递数据。

像这样:

        webView.load(data, mimeType: "application/pdf", characterEncodingName: "UTF-8", baseURL: baseURL)