2

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
}
iosseeker
  • 21
  • 1
  • 3
  • Edit your question and post your code. Have you tried with another word doc file? – Leo Dabus May 18 '19 at 16:16
  • 1
    URL(string: ) initializer usually it is used for remote url. If your document url it is not a fileURL you would need to download your remote resource first – Leo Dabus May 18 '19 at 16:35
  • possible duplicate of https://stackoverflow.com/questions/47140314/how-to-display-remote-document-using-qlpreviewcontroller-in-swift/47141577?r=SearchResults&s=2|28.5943#47141577 – Leo Dabus May 18 '19 at 17:37

1 Answers1

2

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

VRAwesome
  • 4,721
  • 5
  • 27
  • 52