4

I am stuck and don't know what to do. I want to be able to display PDF's within my application using QuickLook. I have imported my PDF files into my project and am using the code below to display it.

    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
    //let bundle = Bundle.main
    let resourcePath = Bundle.main.resourceURL!.appendingPathComponent("Manuals/\(nodeName).pdf").path
    //let path = resourcePath + bundle.path(forResource: nodeName, ofType: "pdf")!
    let url = URL(fileURLWithPath: resourcePath)
    return url as QLPreviewItem
}

The URL which is generated is

"file:///var/containers/Bundle/Application/3923DC1F-386E-4038-A8B1-A855483F4C06/Application%20v1.app/Manuals/CYB.pdf"   

The error I'm getting is

Couldn't issue file extension for url: file:///var/containers/Bundle/Application/3923DC1F-386E-4038-A8B1-A855483F4C06/Application%20v1.app/Manuals/CYB.pdf #PreviewItem

and on the display Im just getting a grey screen with the name of the file and PDF Document under it. Does anybody know what Im doing wrong. I have tired to google the answer but either the solution did not work for me or the answer is outdated.

Any help would be great. If there is anymore information you might need let me know and I can provide that.

Thanks

picciano
  • 22,341
  • 9
  • 69
  • 82
GBSingh
  • 416
  • 5
  • 18
  • 1
    Try copying the `.pdf` into the `Documents` folder, as this has been a known-issue when trying from other folders. – l'L'l Apr 19 '18 at 19:22
  • Hey. Im really sorry but how to I copy this to the documents folder. At the moment my PDF's (I have multiple) just reside in another folder called Manuals. Where is the documents folder I didn't even know there was one? – GBSingh Apr 19 '18 at 19:26

3 Answers3

7

Thanks for everybody who gave their inputs with this issue. I have finally fixed it. It turns out that it was nothing to do with the code that I posted but due to my iPad not running iOS 11.3. For some reason this issue occurs on 11.2.x but got fixed in 11.3. I don't know why I didn't update earlier but its finally working. Hopefully anybody else having this issues finds this useful and doesn't waste a full day trying to figure out why it doesn't work.

Thanks once again.

GBSingh
  • 416
  • 5
  • 18
1

To access a file inside a folder in main Bundle , drag the folder named Manuals to project xcode and select create references folder

enter image description here

//

enter image description here

then access the resource with

let fileUrl = Bundle.main.url(forResource:\(nodeName) ,withExtension:"pdf",subdirectory: "Manuals")  
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Hi. Thanks for your answer but I get the same result. A grey screen with the file title and PDF Document. And also I get the "Couldn't issue file extension for URL" still. Your code removes two lines of mine though so thank you for that. – GBSingh Apr 19 '18 at 19:42
1

It is known issue in iOS 11.2. Possible solution is to copy the file to a temp file in documents directory before displaying it. https://forums.developer.apple.com/thread/91835