Search Posts in my Blog

Wednesday 7 March 2012

Load a PDF File in the iPhone App



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 theQLPreviewController. 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


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //initializing the fileURL object with the URL links to be loaded
    switch (indexPath.row) {
        case 0
            fileURL = [NSURLURLWithString:kStringURLViewControllerPDF];
            break;

      case 1:
            fileURL = [NSURLURLWithString:kStringURLQLPreviewControllerPDF];
            break;
            case 2:
            fileURL = [NSURLURLWithString:kStringURLUIDocumentInteractionControllerPDF];
            break;
    }
    //creating the object of the QLPreviewController
    QLPreviewController *previewController = [[QLPreviewController allocinit];
    
    //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.


#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:


numberOfPreviewItemsInPreviewControllerInvoked 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 

No comments:

Post a Comment