简体   繁体   中英

How to display .doc files in QLPreviewController swift language

I am new to ios developement, i am trying to display file with different format (ex. docx,doc,pdf,csv) , it is working fine with .pdf files but for .doc files its not previewing. enter image description here

This is what showing in simulator

For some .doc files & pdf it works!!1

code :

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
}

First you need to download file and save in local storage. And one more thing is .do file is not supposed. So you have to take sure that your file is .docx or .pdf .

You can try this way :

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
}

iPhone上的.docx文件显示

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM