QLPreviewController无法在iOS 6中运行

时间:2012-09-28 15:57:15

标签: objective-c ios6 qlpreviewcontroller

在iOS 6中,QLPreviewController不再从URL加载PDF。它在iOS 5中运行良好。我已经实现了QLPreviewControllerDataSource方法,如文档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;
}

这在iOS 5中完美运行,但在iOS 6中控制台输出:

Couldn't issue file extension for path: /XTAL/PDF_Instructions/Test_File.pdf

4 个答案:

答案 0 :(得分:8)

您是否尝试过使用fileURLWithPath而不是URLWithString?我还有其他问题已经解决了。

也不确定QLPreviewController是否会处理远程URL。如果没有,您可以下载该文件,然后显示它。

答案 1 :(得分:7)

我从远程网址下载文件并在本地保存,然后使用QLPreviewController显示PDF。在iOS 6中,它正在工作。

首先,我使用以下代码从远程URL保存文件:

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

用于显示Pdf:

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

QLPreviewController委托方法是:

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

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

    return [NSURL fileURLWithPath:local_location];


}

答案 2 :(得分:4)

我遇到类似的问题,似乎可能源于对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

更新:我已经为Apple打开了一个针对iOS 6处理此问题的错误,似乎他们已经将其作为一个错误,因此可能会在不久的将来提供修复。我打开的错误与使用自定义NSURLProtocols进行预览有关,但也可能适用于其他方面。

Link to class

答案 3 :(得分:0)

但请注意,QLPreviewController需要一个到本地资源的URL

您需要先在本地下载并保存PDF文件,然后为本地文件创建正确的文件URL。

相关问题