2011-01-11 131 views
11
When I try to click the menu action button in QLPreviewController the application crashes. 

这是我在委托的方法正在做iPhone-应用程序崩溃的iOS 4.2

- (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)index 
{ 

    NSMutableString*Url = [[NSMutableString alloc] initWithFormat:@"http://10.30.24.21/Documents/abc.doc]; 

    NSURL *fileURL; 
    fileURL = [NSURL URLWithString:Url];// the url of the file which is present in NAS device 
    [Url release]; 
    return fileURL; 
} 

这是崩溃报告

2011-01-11 12:21:36.717 iLink[5548:207] *** Assertion failure in -[UIDocumentInteractionController setURL:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UIDocumentInteractionController.m:1060 
2011-01-11 12:21:36.720 iLink[5548:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIDocumentInteractionController: invalid scheme https. Only the file scheme is supported.' 

当我尝试预览文件目前在本地菜单操作按钮单击不会崩溃。

那里,我将使用

NSURL *fileURL; 
    fileURL = [NSURL fileURLWithPath:filePath];// filePath is local file path. 

我明白,当我们预览本地文件([NSURL fileURLWithPath:文件路径])菜单操作按钮,点击也不会崩溃,,当我们预览从服务器文件([ NSURL URLWithString:Url])菜单操作按钮,点击崩溃。

我有两个问题, 1.我们可以禁用菜单操作按钮吗? 2.有什么办法可以避免使用[NSURL URLWithString:Url]的崩溃?

感谢

回答

2

有什么办法避免碰撞 使用[NSURL URLWithString:URL]?

首先将文件下载到本地文件系统。

+0

应用程序的目标是不要下载文件。它只是用于预览使用URL在NAS设备中存在的文件。 – 2011-01-11 10:09:42

+0

如果不从服务器提取文件,则无法预览文件。至少不是如果你想使用UIDocumentInteractionController。控制器需要本地文件系统上的文件才能运行。 – 2011-01-11 10:13:58

34

需要使用

[NSURL fileURLWithPath:urPath] 
0

顺便说你的发言
NSMutableString*Url = [[NSMutableString alloc] initWithFormat:@"http://10.30.24.21/Documents/abc.doc];有一个错字,错误。你错过了“来表示URL字符串

8

虽然以前的答案已经给出了正确的技术响应,我想更详细地说明结束。

QLPreviewItem的API文档说的URL 必须是文件类型的URL,其中NSURL docs说的意思是“使用file:方案”。

您还可以阅读从Document Interaction Progamming指南,其中提到,让你的QuickLook的是应该给你更多的控制比UIDocumentInteractionController多一点决定它的呈现方式,但它带来了假设您已经在本地获得了一个文件,并且您只需要一种显示方式并(使用QuickLook)将其与AirPrint一起打印出来。

在你的情况下,它可能是最有意义的文件下载到应用程序的Caches目录,然后打开QL预览 - 它已经被预览视图反正下载,所以你还不如抓住它使它也可以打印。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
// Here you'll _probably_ want to use a filename that's got the correct extension...but YMMV 
NSURL *cacheURL = [NSURL fileURLWithPath:[[paths objectAtIndex:0] stringByAppendingPathComponent: @"current.pdf"]]; 

现在把你的原始URL并下载内容并保存在cacheURL。如果你使用ASIHTTPRequest,它看起来像这样:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:originalURL]; 
[request setDownloadDestinationPath:[cacheURL path]]; 
// Yup. You could optimize here if needed... 
[request startSynchronous]; 

然后使用文件URL在你的QuickLook的看法...

- (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)index { 
    // Assuming you've saved this somewhere, etc... 
    return self.cacheURL; 
} 

在这种情况下,你看看从每一个新的PDF您的NAS 将覆盖前一个(同名,同一目录),因此您限制了您的磁盘使用情况。显然,有很多方法可以解决这个问题,所以请选择适合您的方法。然而,关键是要在本地下载文件,并至少保留它直到QL视图被解除为止。

0

我能够使用HTTP方案显示PDF文件。当用户按下打印按钮时,应用程序崩溃。所以我需要一种方法来显示HTTP上的PDF,并且需要禁用打印按钮。禁止在viewDidAppear一个子键下面是我的代码

@interface PreviewController : QLPreviewController 
@end 

@implementation PreviewController 
-(void) viewDidAppear:(BOOL)animated { 
    self.navigationItem.rightBarButtonItem = nil; 
} 
@end 

,并作为代码:

// use the new class 
QLPreviewController *previewController = [[PreviewController alloc] init]; 
previewController.delegate = self; 
previewController.dataSource = self; 
[self.navigationController pushViewController:previewController animated:FALSE];