Search code examples
iphoneobjective-ciosios4quicklook

iOS - Opening a PDF via Quicklook without using UIScrollView


I am trying to open a PDF via the QuickLook framework without using UIScrollView...

I believe I'm missing something...

Where I believe I'm going wrong is that I need to use a QLPreviewController and on the QLPreviewController is a dataSource that has to conform to QLPreviewItem. The documentation states that NSURL does conform to QLPriewItem so I'm setting the preview.dataSource to an NSURL which is throwing an error:

[NSURL numberOfPreviewItemsInPreviewController:]: unrecognized selector sent to instance

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL numberOfPreviewItemsInPreviewController:]: unrecognized selector sent to instance 0x5b5f200'

Which makes me think that NSURL does not conform.

all the code I think is necessary...

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

    return YES;
}

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

    return [documents count];
}

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

    return [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[documents objectAtIndex:index] ofType:nil]];
}

- (void)pushPDF {

    QLPreviewController *preview = [[QLPreviewController alloc] init];
    preview.dataSource = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"MCIT_Quiz" ofType:@"pdf"]];
    //preview.currentPreviewItemIndex = 0;
    [self presentModalViewController:preview animated:YES];
    [preview release];
}

Solution

  • I ended up just creating another class to hold my values and use as a datasource, a bit quick and dirty but it works.

    //
    //  documentList.h
    //
    
    #import <Foundation/Foundation.h>
    #import <QuickLook/QuickLook.h>
    
    
    @interface DocumentList : NSObject <QLPreviewControllerDataSource, QLPreviewControllerDelegate> {
        NSArray *documents;
    }
    
    @property (nonatomic, retain) NSArray *documents;
    
    -(void)createList;
    -(NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller;
    - (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index;
    
    @end
    

    inserting text to break up the files

    //
    //  documentList.m
    //
    
    #import "DocumentList.h"
    
    @implementation DocumentList
    
    @synthesize documents;
    
    -(void) createList {
    
        documents = [[NSArray arrayWithObjects:@"Quiz.pdf", nil] retain];
    }
    
    -(NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller {
    
        return [documents count];
    }
    
    - (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index {
    
    return [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[documents objectAtIndex:index] ofType:nil]];
    }
    
    @end