QLPreviewController not working in iOS 6
Asked Answered
T

4

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
Tamandua answered 28/9, 2012 at 15:57 Comment(3)
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 host portion.Aaronaaronic
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.Delative
Did you find a solution? if so please share it or accept an answer. ThanksNatter
Z
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];


}
Zig answered 13/12, 2012 at 13:5 Comment(3)
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 ...Leavings
@SébastienNussbaumer I didnt get this error in my project. once check your path.Zig
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 :)Leavings
A
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.

Aaronaaronic answered 8/10, 2012 at 15:11 Comment(2)
fileURLWithPath fixed my problem! Thanks!Paralyse
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.Tlingit
Z
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];


}
Zig answered 13/12, 2012 at 13:5 Comment(3)
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 ...Leavings
@SébastienNussbaumer I didnt get this error in my project. once check your path.Zig
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 :)Leavings
D
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

Delative answered 9/10, 2012 at 15:23 Comment(1)
Still no update from Apple, but was able to get a work around working using UIWebView and overriding urlprotocol to load the fileDelative
E
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.

Ecclesia answered 7/11, 2017 at 6:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.