Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QLPreviewController not working in iOS 6

In iOS 6 the QLPreviewController no longer loads a PDF from a URL. It works fine in iOS 5. I have implemented the QLPreviewControllerDataSource methods as documented here.

#pragma mark - QLPreviewControllerDataSource
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
    return 1;
}

- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index;
{
    NSURL *fileURL = [NSURL URLWithString:@"http://www.bliley.net/XTAL/PDF_Instructions/Test_File.pdf"];
    return fileURL;
}

This works perfectly in iOS 5, however in iOS 6 the console outputs:

Couldn't issue file extension for path: /XTAL/PDF_Instructions/Test_File.pdf
like image 813
benr75 Avatar asked Sep 28 '12 15:09

benr75


3 Answers

Have you tried using fileURLWithPath instead of URLWithString? I had other issues that were fixed by doing so.

Also not sure if QLPreviewController will handle remote URLs. If not, you could download the file and then display it.

like image 161
valheru Avatar answered Nov 20 '22 20:11

valheru


I downloaded the file from remote url and saved locally, then I display the PDF using the QLPreviewController .In iOS 6 its working.

First i saved the file from remote url using the following code :

    NSString *local_location;

    NSString *path = [[NSBundle mainBundle] pathForResource:@"sampleData" ofType:@"plist"];
        path = NSTemporaryDirectory();
    local_location= [path stringByAppendingPathComponent:[NSString stringWithFormat:@"My_Invoice.pdf"]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString: remoteurl]];
        [request setDownloadDestinationPath:local_location];
        [request startSynchronous];

For showing the Pdf :

QLPreviewController* preview = [[QLPreviewController alloc] init];
        preview.dataSource = self;
        [self presentModalViewController:preview animated:YES];

QLPreviewController delegate methods are :

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

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

    return [NSURL fileURLWithPath:local_location];


}
like image 41
Suresh Avatar answered Nov 20 '22 19:11

Suresh


I am having a similar issue and seems like it might stem from a stricter enforcement of the file-type URL of QLPreviewItem

@property (readonly) NSURL *previewItemURL;
Discussion
This property is used by a Quick Look preview controller to get an item’s URL. In typical use, you would implement a getter method in your preview item class to provide this value.

The value of this property must be a file-type URL.

If the item is not available for preview, this property’s getter method should return nil. In this case, the Quick Look preview controller displays a “loading” view.

Availability
Available in iOS 4.0 and later.
Declared In
QLPreviewItem.h

UPDATE: I have opened a bug with Apple dealing with this issue for iOS 6 and it seems they have aced it as a bug so may offer a fix in the near future. The bug I opened had to do with using custom NSURLProtocols for the preview, but may apply to other aspects as well.

Link to class

like image 4
MikeIsrael Avatar answered Nov 20 '22 18:11

MikeIsrael