Hi Friends today we will learn how to use QuickLook framework to view file of extension type pdf,rtf,ppt,doc,png in iOS.

Steps:

  1. Create a new Xcode project (Lets say QuickLook).
  2.  Now go to storyBoard and take 7 buttons. And make connection for them.

screen-shot-2017-02-04-at-3-09-05-pm

  1. Now import QuickLook framework into  your project.

screen-shot-2017-02-04-at-12-57-13-pm

  1.  Now go to Your ViewController.m and  import the header. declare one array and integer variable.

#import “ViewController.h”

#import <QuickLook/QuickLook.h>

@interface ViewController ()<QLPreviewControllerDelegate,QLPreviewControllerDataSource>

@end

@implementation ViewController{

    NSString *path;

    NSArray *fileNamesArray;

    NSInteger btnTag;

}

– (void)viewDidLoad {

    [super viewDidLoad];

    fileNamesArray=[[NSArray alloc] initWithObjects:@”sample.pdf”,@”sample.pptx”,@”sample.doc”,@”sample.rtf”,@”sample.jpeg”,@”sample.csv”, nil];

    // Do any additional setup after loading the view, typically from a nib.

}

5. Now we will write the method to initialize the QLPreviewController .

-(void)initiateQuickLoookController{

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

    previewController.delegate=self;

    previewController.dataSource=self;

    [self presentViewController:previewController animated:YES completion:nil];

}

6 . Now Add all the files in your project. You can download the whole file from the project link given below.

7.  Write the button action method .

– (IBAction)buttonAction:(id)sender {

    btnTag = 0;

    switch ([sender tag]) {

        case 0:

            path = [[NSBundle mainBundle] pathForResource:@”sample”

                                                   ofType:@”pdf”];

            break;

        case 1:

            path = [[NSBundle mainBundle] pathForResource:@”sample”

                                                   ofType:@”rtf”];

            break;

        case 2:

            path = [[NSBundle mainBundle] pathForResource:@”sample”

                                                   ofType:@”pptx”];

            break;

        case 3:

            path = [[NSBundle mainBundle] pathForResource:@”sample”

                                                   ofType:@”jpeg”];

            break;

        case 4:

            path = [[NSBundle mainBundle] pathForResource:@”sample”

                                                   ofType:@”doc”];

            break;

        case 5:

            path = [[NSBundle mainBundle] pathForResource:@”sample”

                                                   ofType:@”csv”];

            break;

        case 6:

            btnTag = 6;

            break;

            

        default:

            break;

    }

   

[self initiateQuickLoookController];

}

7. Now define the dataSource method .

#pragma mark – DataSource Methods

– (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{

    if (btnTag == 6) {

        return [fileNamesArray count];

    }

    return 1;

}

– (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{

    

    

     if(btnTag==6)

     {

     NSString *allPath=[[NSBundle mainBundle] pathForResource:[fileNamesArray objectAtIndex:index] ofType:nil];

     return [NSURL fileURLWithPath:allPath];

     }

     

   

     return [NSURL fileURLWithPath:path];

    

}

8. Define the delegate methods.

#pragma mark – delegate methods

– (BOOL)previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url forPreviewItem:(id <QLPreviewItem>)item

{

    return YES;

}

9. Now run the application and see the desired output.

 

You can download the whole project from github.

Please do share and comment your reviews.

Advertisements