从Webview下载pdf文件

时间:2019-08-16 14:41:58

标签: swift pdf webview

这是我从Webview下载pdf文件的代码:

func savePDF() {
    let fm = FileManager.default
    guard let pdfURL = fm.documentsURL?.appendingPathComponent("\(self.transRefId).pdf") else { return }
    print("pdfURL:\(pdfURL)")

    if fm.fileExists(atPath: pdfURL.path) {
        do {
            try fm.removeItem(at: pdfURL)
        } catch let fileError {
            let ac = UIAlertController(title: nil, message: fileError.localizedDescription, preferredStyle: .alert)
            ac.addAction( UIAlertAction(title: "OK", style: .default, handler: nil) )
            self.present(ac, animated: true, completion: nil)
            return
        }
    }

    let renderer = HTML2PDFRenderer()
    renderer.render(webView: self.webView!, toPDF: pdfURL, paperSize: .a4, paperMargins: UIEdgeInsets.init(top: 30, left: 0, bottom: 0, right: 0)) {
        url, error in

        if let error = error {
            DispatchQueue.main.async {
                let ac = UIAlertController(title: nil, message: error.localizedDescription, preferredStyle: .alert)
                ac.addAction( UIAlertAction(title: "OK", style: .default, handler: nil) )
                self.present(ac, animated: true, completion: nil)
            }
            return
        }

        print("URL: \( url?.path ?? "" )")
        DispatchQueue.main.async {
            let m = NSLocalizedString("Successfully", comment: "")
            let ac = UIAlertController(title: nil, message: m, preferredStyle: .alert)
            ac.addAction( UIAlertAction(title: "OK", style: .default, handler: nil) )
            self.present(ac, animated: true, completion: nil)
        }
    }
}

并且pdf文件已成功下载到应用程序文件夹下的“文件”应用程序中,但是,当我尝试打开从“文件”应用程序中保存的pdf文件时,当我双击该pdf文件时,它将自动链接回我的应用。我想执行以下操作,当我双击pdf文件时,它将使用默认阅读器而不是链接到我的应用程序打开pdf文件。怎么做?有人可以帮我吗

0 个答案:

没有答案