如何以QLPreviewController快速语言显示.doc文件

时间:2019-05-18 15:38:48

标签: ios swift doc qlpreviewcontroller

我是ios开发的新手,我正在尝试显示不同格式的文件(例如docx,doc,pdf,csv),它可以与.pdf文件正常工作,但对于.doc文件,其无法预览。{{ 3}}

这是模拟器中显示的内容

对于一些 .doc文件和pdf来说,它可以正常工作!1

代码:

override func viewDidLoad() {
    super.viewDidLoad()
}
@IBAction func handlePress(){

    let previewController = QLPreviewController()
    previewController.dataSource = self
    present(previewController, animated: true)
}
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
    return 3
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
    guard let url = URL(string: "**urlpath**/q.DOC") else {
       fatalError("Could not load")
    }

    return url as QLPreviewItem
}

1 个答案:

答案 0 :(得分:0)

首先,您需要下载文件并保存在本地存储中。还有一件事情是.do文件不应该使用。因此,您必须确保文件为.docx.pdf

您可以尝试这种方式:

func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {

    // ---- First way -----
    guard let strPath =  Bundle.main.path(forResource: "demo.docx", ofType: nil) else { return "" as! QLPreviewItem }
    //guard let strPath =  Bundle.main.path(forResource: "demo", ofType: "docx") else { return "" as! QLPreviewItem }
    let filePath = "file://\(strPath)"
    let url = URL(string: filePath)

    // ---- Second way -----
    guard let strPath =  Bundle.main.path(forResource: "demo.docx", ofType: nil) else { return "" as! QLPreviewItem }
    //guard let strPath =  Bundle.main.path(forResource: "demo", ofType: "docx") else { return "" as! QLPreviewItem }
    let url = URL(fileURLWithPath: strPath)

    return url as QLPreviewItem
}

.docx file display on iPhone