我正在尝试创建一个将 pdf 保存在本地存储中的应用程序,我希望该 pdf 在 Adobe Reader 中打开。
此外,我不想在任何 WebView 中打开 pdf。事实上,我希望在 Adobe Reader 中打开 pdf。
我搜索了很多但没有成功,所有示例都是在 web View 中打开 pdf。
下面是我的代码:
@IBAction func savePDF(_ sender: Any) {
let pdfData = Data(base64Encoded: FilePDF, options: .ignoreUnknownCharacters)
let array = pdfData?.withUnsafeBytes
{
[UInt8](UnsafeBufferPointer(start: $0, count: (pdfData?.count)!))
}
let data = NSData(bytes: array, length: (array?.count)!);
let tmpDir = NSTemporaryDirectory()
// data.write(toFile: tmpDir.appending("HandyHR_File_YEAR.pdf"), atomically: true)
print(array!)
print(data)
print(tmpDir)
data.write(toFile: tmpDir.appending("HandyHR_File_YEAR.pdf"), atomically: true)
}
变量 tmpDir
在本地存储中存储 pdf 的路径。
如有任何帮助,我们将不胜感激。
提前致谢。
最佳答案
一种选择是使用 UIDocumentInteractionController
显示文件:
包含 Controller 的实例变量:
fileprivate lazy var docInteractionController: UIDocumentInteractionController = {
return UIDocumentInteractionController()
}()
呈现预览:
let tmpDir = NSTemporaryDirectory()
let fileName = "HandyHR_File_YEAR.pdf"
// Save file
let url = URL(fileURLWithPath: tmpDir).appendingPathComponent(fileName)
docInteractionController.url = url
docInteractionController.delegate = self
if docInteractionController.presentPreview(animated: true) {
// Successfully displayed
} else {
// Couldn't display
}
您还可以实现委托(delegate)来指定呈现预览的 View Controller :
extension YourViewController: UIDocumentInteractionControllerDelegate {
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
// I tend to use 'navigationController ?? self' here but depends on implementation
return self
}
}
关于iOS Swift - 如何显示 pdf 文件,该文件位于 adobe reader 的本地存储中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45503055/