iosseeker
iosseeker

Reputation: 21

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
}

Upvotes: 2

Views: 3113

Answers (1)

VRAwesome
VRAwesome

Reputation: 4803

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
}

.docx file display on iPhone

Upvotes: 2

Related Questions