4
class ViewController: UIViewController {
    let quickLookController = QLPreviewController()

    override func viewDidLoad() {
        super.viewDidLoad()

        quickLookController.dataSource = self
        quickLookController.delegate = self
    }

    @IBAction func buttonAction(_ sender: Any) {
        present(quickLookController, animated: true, completion: nil)

        quickLookController.reloadData()
        quickLookController.refreshCurrentPreviewItem()
    }
}

extension ViewController: QLPreviewControllerDataSource,QLPreviewControllerDelegate {

    func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
        return 1
    }

    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
        let path = Bundle.main.path(forResource: "AppCoda-Ppt.ppt", ofType: nil)

        let url = NSURL(fileURLWithPath: path!)

        return url
    }
}

enter image description here

rmaddy
  • 314,917
  • 42
  • 532
  • 579

3 Answers3

4

This is a bug in iOS 11.2. Please file it at bugreport.apple.com if you want to be kept up to date on its status. A workaround is not to store your ppt file in your app bundle. Use a different location somewhere in your container, such as your Application Support directory.

Thomas Deniau
  • 2,488
  • 1
  • 15
  • 15
  • At launch or when you want to use it, copy it out of the bundle to the app file system. The cache directory is a good place. – Warren Burton Jan 13 '18 at 18:03
  • Has Apple confirmed you that it is a bug in iOS11.2? – enagra Jan 17 '18 at 18:08
  • rdar://35467170 – Thomas Deniau Jan 18 '18 at 19:30
  • Didn't get any search results for the bug number you specified, is this still a bug because I'm having the exact same issue except I'm reading files from cache as opposed to main bundle? – Astronought Feb 23 '18 at 19:27
  • If this is not from the bundle, please file a separate bug. Are any sandbox violations being logged? – Thomas Deniau Feb 24 '18 at 20:12
  • It's strange, OP can't load a file from main bundle, but I'm able to load a file form main bundle I just can't load a file form the cache directory, it simply says the file cannot be found. Even if I copy the exact path etc. of the file it fails to find it, I've tested this on both the xcode simulator and on a iphone device. – Astronought Feb 26 '18 at 19:15
1

Answer from @Thomas is correct (THANK YOU!) For Swift 3 you can do something like this in the previewItemAt:index method to cache the file on demand:

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

    guard let CacheDirURL = try? FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true) else {
        print("Can't get cache dir URL")
        return NSURL(fileURLWithPath: "FILE_NOT_FOUND")
    }

    let fileUrl = CacheDirURL.appendingPathComponent("cachedFileName.pdf")

    if !FileManager.default.fileExists(atPath: fileUrl.path) {
        if let sourceUrl = Bundle.main.url(forResource: "Pioneer_UAS_Policy_Aug_2016", withExtension: "pdf") {
            print("Copying file from bundle \(sourceUrl.path)")
            try? FileManager.default.copyItem(at: sourceUrl, to: fileUrl)
        }
    }

    return NSURL(fileURLWithPath: fileUrl.path)

}
biomiker
  • 3,108
  • 1
  • 29
  • 26
0

You have to copy the file to the i.e. temporary directory and read the file from there.

saltwat5r
  • 1,107
  • 13
  • 21