11

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
pizza247
  • 3,869
  • 7
  • 33
  • 47
benr75
  • 312
  • 4
  • 9
  • Doesn't it actually require using a local file URL? It looks to me like you are using a remote url and it's erroring with everything after the http://host/ portion. – valheru Oct 03 '12 at 20:47
  • someone told me that in ios 6 they implemented a stricter check for this method that the url most start with 'file://', but I can't find any documentations on it. If anyone knows of a reference, please post. – MikeIsrael Oct 09 '12 at 15:15
  • Did you find a solution? if so please share it or accept an answer. Thanks – Daniel Benedykt May 03 '13 at 14:16

4 Answers4

8

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.

valheru
  • 2,552
  • 3
  • 20
  • 40
  • 1
    fileURLWithPath fixed my problem! Thanks! – Josip B. Oct 18 '12 at 09:17
  • I have an application in App Store already. It works fine in iOS 5. But it doesn't work any more. I need to change everything to make it work? What a great "upgrading" by Apple. – Bagusflyer Nov 30 '12 at 12:54
7

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];


}
Suresh
  • 1,015
  • 9
  • 16
  • 2
    Just my two cents : you also get the "Couldn't issue file extension for path" error if the file pointed by the URL does not exists. So if you download the file and give then a wrong URL to QLPreviewController you'll get this same error. Why would you do that ? Well silly bugs do happen ... – Sébastien Nussbaumer Mar 06 '13 at 16:53
  • @SébastienNussbaumer I didnt get this error in my project. once check your path. – Suresh Mar 08 '13 at 06:43
  • 1
    Sorry I didn't mean to say that this happened in your project, just that it happened in mine and wanted to let know other readers that checking that the file exists is a good idea :) – Sébastien Nussbaumer Mar 08 '13 at 10:45
4

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

MikeIsrael
  • 2,871
  • 2
  • 22
  • 34
  • Still no update from Apple, but was able to get a work around working using UIWebView and overriding urlprotocol to load the file – MikeIsrael Apr 18 '13 at 07:47
0

But note that QLPreviewController expects a URL to a local resource

You would need to download and save the PDF file locally first and then create a proper file URL to the local file.

Ashish
  • 2,977
  • 1
  • 14
  • 32