2

I created a new view controller in storyboard. Connected it to the ListViewController. The ListViewController fills in the correct data to aboutDict[String:Any]. The fileURLs[] is built and func numberOfPreviewItems(in: QLPreviewController) -> Int is called - the debugger shows the fileURLs[] is as expected:

Printing description of self.fileURLs: ▿ 1 element - 0 : file:///Users/kent/Library/Developer/CoreSimulator/Devices/5E23825C-DF99-455A-BEB1-F73398E7759F/data/Containers/Bundle/Application/307ED7DF-C07C-4C0A-BA78-938BABE7C22C/WINSystemInfo.app/ID-51A_E_PLUS2.pdf`

But func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem and func previewController(_ controller: QLPreviewController, shouldOpen url: URL, for item: QLPreviewItem) -> Bool are not called.

I get a nice ViewController that was pushed into the navigation stack with the correct name for the back button, and a gray page with centered text "No file to preview".

On pressing 'back' button, func previewControllerWillDismiss(_ controller: QLPreviewController) is called. So some of the delegate and datasource functions are being called.

I must be missing something simple...

I attached my class FileViewController.swift below:

class FileViewController: QLPreviewController, QLPreviewControllerDelegate, QLPreviewControllerDataSource {

    var aboutDict = [String: Any]()

    // QuickLook data
    var fileURLs = [QLPreviewItem]()

    // MARK: - Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()
        let filename = aboutDict["filename"] as! String?
        let filetype = aboutDict["filetype"] as! String?

        title = aboutDict["title"] as! String?
        dataSource = self
        delegate = self

        if let fileUrl = Bundle.main.url(forResource: filename, withExtension: filetype, subdirectory: nil, localization: nil)
        {
            let filePreview = fileUrl as QLPreviewItem
            fileURLs.append(filePreview)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Preview controller datasource  functions

    func numberOfPreviewItems(in: QLPreviewController) -> Int {
        return fileURLs.count
    }

    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
        return fileURLs[index]
    }

    // MARK: - Preview controller delegate functions

    func previewControllerWillDismiss(_ controller: QLPreviewController) {
        debug("previewControllerWillDismiss")
    }

    func previewController(_ controller: QLPreviewController, shouldOpen url: URL, for item: QLPreviewItem) -> Bool {
        return true
    }
}

What am I missing?

Thank you.

Kent
  • 1,374
  • 1
  • 15
  • 29

1 Answers1

0

I just had this problem and it was simply because I had forgotten to set my datasource on the QLPreviewController...

Epaga
  • 38,231
  • 58
  • 157
  • 245