iOS Swift - 如何显示 pdf 文件,该文件位于 adobe reader 的本地存储中

标签 ios iphone swift xcode pdf

我正在尝试创建一个将 pdf 保存在本地存储中的应用程序,我希望该 pdf 在 Adob​​e Reader 中打开。

此外,我不想在任何 WebView 中打开 pdf。事实上,我希望在 Adob​​e 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/

相关文章:

ios - Swift:应用程序被杀死时运行特定代码?

iphone - 适用于 iOS 的富文本编辑器库

ios - 我的项目的 iOS 部署目标设置为 iOS 9.0,我想将 Reality Kit 集成到 iOS 13.0 及更高版本的应用程序中

iphone - loadView 和 viewDidLoad 有什么区别?

iphone - 移动行后更新 UITableViewCell 标签属性

ios - Xcode 中的线程 1 : EXC_BAD_INSTRUCTION (code = EXC_1386_INVOP, subside = 0x0)

ios - 我应该为 ios 开发 native 使用哪种图像格式? SVG 还是 PNG?

ios - 在 iOS 模拟器上测试 IAP 时出现错误 {NSLocalizedDescription=Cannot connect to iTunes Store}

iphone - UIPasteboard 粘贴板 App iOS iPhone

swift - 使用函数的返回值 - Swift