Appcoda

Using Quick Look Framework for Previewing Documents

Several treasures like not so well-known frameworks and libraries can be found in the iOS SDK. Most of them can be proved to be real gems and truly useful tools, saving you from hours of extra work. One of them is the Quick Look Framework, and even if you’ve never used it before, it’s easy to understand what’s all about just from its name; it provides previewing capabilities for documents that an app handles. Like most people, I wasn’t aware of it until I had to use it for first time; then I appreciated its existence.

The Quick Look Framework can be used quite easily, and it can open specific types of documents for previewing. Those types are:

  • iWork documents (Pages, Numbers and Keynote)
  • Microsoft Office documents (as long as they’ve been created with Office 97 or any other newer version)
  • PDF files
  • Images
  • Text files
  • Rich-Text Format documents
  • Comma-Separated Value files (csv)

As you might be thinking right now, the Quick Look framework can become really handy if your app is dealing with files of any of the above file types and you want your users to be able to preview their content. Except for that, the Quick Look framework provides sharing options as well. An activity controller (UIActivityViewController) can be presented for sending or sharing the previewed document.

Becoming a bit more specific and technical now, the developer’s main task when using the Quick Look framework is to provide a datasource that the preview controller will use in order to eventually open the proper document(s) for previewing. This datasource is actually a list of NSURL objects specifying the path to each document, and apparently it can be either a locally stored, or a remote document. Locally stored files include those that reside either in the documents directory, or in the bundle of the app.

The Quick Look framework provides a view controller named QLPreviewController (Quick Look Preview Controller) for quick looking a document. This view controller can be presented to the app modally, or to be pushed to the navigation stack if the app is navigation-based. It’s the main component of the framework, and once it’s presented it provides the sharing options, as well as the possibility to switch to another document without having to dismiss it by displaying a list with the whole set of available documents. Additionally, the framework requires the implementation of two datasource methods that belong to the QLPreviewControllerDataSource protocol in order for the preview to properly work. Besides that, there’s also the QLPreviewControllerDelegate protocol as well, which you can optionally implement and gain that way more control over the functionalities of the Quick Look framework.

We’ll have the chance to discuss all the above in details, and as we usually do, we’ll use a demo app for this purpose. However, before we go to the insights of the Quick Look framework, let’s have a quick tour to what our demo app is all about.

Continue reading my tutorial on Appcoda