4

Is there a way to extend the Quick Look Framework on iOS to handle an unknown file type like on Mac? I don't want to have to switch to my app to preview the file, much like viewing image files in email or iMessage. I would like to remove the step of having to select what app to use to open the file.

On Mac they call it a Quick Look Generator, but I can't find a way to do it on iOS

n6xej
  • 461
  • 5
  • 15

2 Answers2

2

This is how you use Quick Look Framework in iOS

Xcode 8.3.2. Swift 3

First goto Build Phases and add new framework QuickLook.framework under Link Binary with Libraries.

Next import QuickLook in your ViewController Class

Next set delegate method of QuickLook to your ViewController class to access all the methods of QUickLook.framework (see below).

class ViewController: UIViewController , QLPreviewControllerDataSource {
    }

Next create instance of QLPreviewController in your class as below:

let quickLookController = QLPreviewController()

Now set datasource in your viewdidload method:

override func viewDidLoad() {
        super.viewDidLoad()

        quickLookController.dataSource = self
}

Now create an fileURLs array to store all the documents path which you need to pass later to QLPreviewController via delegate methods.

var fileURLs = [URL]()

Now add below methods to your class to tell QLPreviewController about your total number of documents.

func numberOfPreviewItemsInPreviewController(controller: QLPreviewController) -> Int {
            return fileURLs.count
        }

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

@available(iOS 4.0, *)
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
            return fileURLs.count
        }

Finally the method which shows your docs. You can also check if the type of document you want to Preview is possible to preview or not as below.

func showMyDocPreview(currIndex:Int) {

        if QLPreviewController.canPreview(fileURLs[currIndex] as QLPreviewItem) {
            quickLookController.currentPreviewItemIndex = currIndex
            navigationController?.pushViewController(quickLookController, animated: true)
        }
    }
Prashant Gaikwad
  • 3,493
  • 1
  • 24
  • 26
  • I have data, not the URLs so how to show it? I get this error for passing base64 pdf data: Could not cast value of type '__NSCFString' (0x2079d5f90) to 'QLPreviewItem' – Md Rais Jun 26 '19 at 07:34
1

For now, if you want to show a preview of a file of a type not handled by the standard QLPreviewController, you have to write something yourself in your own app. You cannot write a custom Quick Look plugin like you can on the Mac.

woz
  • 10,888
  • 3
  • 34
  • 64