In todays session we will learn how to launch a PDF file present in the server into our iPhone application.

For this you require the QuickLook Framework and its class called as the QLPreviewController. With the help of this class you can view documents like

a) iWork Documents
b) Microsoft Office Documents
c) RTF documents
d) PDF Files
e) Images
f) Text Files
g) CSV (Comma separated files)

For this tutorial i have just displayed PDF files, the file could be present any where in the server or in the app bundle. But since in maximum of the application the PDF files are loaded from the server i shall do the same in this demo.

Step 1: Open Xcode and create a view which looks like the one given below

Step 2: Add the Quick Look framework into your project and import it into your view controller.h file, create the object of the QLPreviewController in the table views did select row at index method and set its dataSource and delegate methods

  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  2.  
  3.     //initializing the fileURL object with the URL links to be loaded
  4.     switch (indexPath.row) {
  5.         case 0:
  6.             fileURL = [NSURL URLWithString:kStringURLViewControllerPDF];
  7.             break;
  8.  
  9.       case 1:
  10.             fileURL = [NSURL URLWithString:kStringURLQLPreviewControllerPDF];
  11.             break;
  12.             case 2:
  13.             fileURL = [NSURL URLWithString:kStringURLUIDocumentInteractionControllerPDF];
  14.             break;
  15.     }
  16.     //creating the object of the QLPreviewController
  17.     QLPreviewController *previewController = [[QLPreviewController alloc] init];
  18.  
  19.     //settnig the datasource property to self
  20.     previewController.dataSource = self;
  21.  
  22.     //pusing the QLPreviewController to the navigation stack
  23.     [[self navigationController] pushViewController:previewController animated:YES];
  24.     [previewController release];
  25. }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    //initializing the fileURL object with the URL links to be loaded
    switch (indexPath.row) {
        case 0:
            fileURL = [NSURL URLWithString:kStringURLViewControllerPDF];
            break;

      case 1:
            fileURL = [NSURL URLWithString:kStringURLQLPreviewControllerPDF];
            break;
            case 2:
            fileURL = [NSURL URLWithString:kStringURLUIDocumentInteractionControllerPDF];
            break;
    }
    //creating the object of the QLPreviewController
    QLPreviewController *previewController = [[QLPreviewController alloc] init];

    //settnig the datasource property to self
    previewController.dataSource = self;

    //pusing the QLPreviewController to the navigation stack
    [[self navigationController] pushViewController:previewController animated:YES];
    [previewController release];
}

Code Explanation: The maximum coding is very simple and is given in the comments i am initializing the fileURL object which is the object of NSURL class which will be returned in the QLPreviewController datasource later to load the pdf files/

Step 3: Add the QLPreviewController datasource method which will do all the hardwork for you and launch the PDF for you.

  1. #pragma mark QLPreviewControllerDataSource
  2.  
  3. // Returns the number of items that the preview controller should preview
  4. - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController
  5. {
  6.     return 30;
  7. }
  8.  
  9. // returns the item that the preview controller should preview
  10. - (id)previewController:(QLPreviewController*)previewController previewItemAtIndex:(NSInteger)idx
  11. {
  12.     return fileURL;
  13. }
#pragma mark QLPreviewControllerDataSource

// Returns the number of items that the preview controller should preview
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController
{
    return 30;
}

// returns the item that the preview controller should preview
- (id)previewController:(QLPreviewController*)previewController previewItemAtIndex:(NSInteger)idx
{
    return fileURL;
}

Code Explanation:

numberOfPreviewItemsInPreviewController: Invoked when the Quick Look preview controller needs to know the number of preview items to include in the preview navigation list. (required)

previewItemAtIndex: Invoked when the Quick Look preview controller needs the preview item for a specified index position. (required)

Step 4: Add the view into the iPhone window with the help of the navigation controller, and then add the navigation controller into the iPhone window to add navigation and in case if you dont want navigation you may skip the adding the navigation controller.

Step 5: Launch the application and view the PDF's

you may also zoom in and out and even print the PDF.

But it is advised to use the UIDocumentInteractionController but its a bit complex so i came up with this easy solution.

Download the source code from here.

Let me know in case if you have any queries regarding the same until then Happy iCoding and have a great Day.