如何从文档目录Swift 3中打开pdf文件

时间:2017-07-04 15:10:17

标签: ios swift

我将pdf存储在文档目录中,我想打开此pdf文件

let documentsURL = try! FileManager().url(for: .documentDirectory,
                                          in: .userDomainMask,
                                          appropriateFor: nil,
                                          create: true)

let path = documentsURL.appendingPathComponent("myFile.pdf")

1 个答案:

答案 0 :(得分:0)

您可以使用QLPreviewController将QuickLook导入控制器

打开pdf文件
import QuickLook

func openQlPreview() {
        let previoew = QLPreviewController.init()
        previoew.dataSource = self
        previoew.delegate = self
        self.present(previoew, animated: true, completion: nil)
    }



extension UIViewController : QLPreviewControllerDelegate , QLPreviewControllerDataSource{

    func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
        return 1
    }

    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
         //  pass your document url here
        return path as QLPreviewItem
    }

    func previewController(_ controller: QLPreviewController, shouldOpen url: URL, for item: QLPreviewItem) -> Bool {
        return true
    }


}