Monday, April 4, 2016

Objective C Quick Look Preview Controller

When we need to display documents, we go for UIWebView and give the absolute path or the downloaded local path. However, UIWebView can not render properly docx, pptx and xlsx files. For this Apple has given this QLFramework, which is designed for rendering these files.


As per Apple Documentation,

A Quick Look preview controller can display previews for the following items:

  • iWork documents
  • Microsoft Office documents (Office ‘97 and newer)
  • Rich Text Format (RTF) documents
  • PDF files
  • Images
  • Text files whose uniform type identifier (UTI) conforms to the public.text type (see Uniform Type Identifiers Reference)
  • Comma-separated value (csv) files

Here is the QLPreviewController usage.

    QLPreviewController *previewController=[[QLPreviewController alloc]init];



    [previewController setAccessibilityValue:self.pptPath];

    [self.navigationController presentViewController:previewController animated:YES 

    [previewController.navigationItem setRightBarButtonItem:nil];

Here are the required QLPreviewControllerDataSource and QLPreviewControllerDelegate methods.

 #pragma mark - data source methods

 /***     Data source methods  ****/
 //– numberOfPreviewItemsInPreviewController:
 //– previewController:previewItemAtIndex:

 - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
    return 1;

 - (id <QLPreviewItem>)previewController:(QLPreviewController *)controller 
    return [NSURL fileURLWithPath:self.pptPath];


 #pragma mark - delegate methods

 - (BOOL)previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url 
                                                                             forPreviewItem:(id <QLPreviewItem>)item
    return YES;


We can preview any number of files at a time by passing QLPreviewItem objects.


Hope this post is useful. Feel free to comment in case of any queries.