简体   繁体   English

点击QLPreviewController ios 4.2中的菜单操作按钮,iPhone-应用程序崩溃

[英]iPhone- application crashes on click of menu action button in QLPreviewController ios 4.2

 When I try to click the menu action button in QLPreviewController the application crashes.

This is what I'm doing in delegate method 这就是我在委托方法中所做的

- (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;
}

This is the crash report 这是崩溃报告

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.'

When I try to preview file present locally menu action button click is not crashing. 当我尝试在本地预览文件时,菜单操作按钮单击不会崩溃。

there I'll be using 那里我会用

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

I understood that when we preview local files ([NSURL fileURLWithPath:filePath]) menu action button click is not crashing ,, when we preview files from server ([NSURL URLWithString:Url]) menu action button click crashes. 我明白当我们预览本地文件时([NSURL fileURLWithPath:filePath])菜单操作按钮点击不会崩溃,当我们从服务器预览文件时([NSURL URLWithString:url])菜单操作按钮点击崩溃。

I have two questions, 1. Can we disable the menu action button? 我有两个问题,1。我们可以禁用菜单操作按钮吗? 2. Is there any way to avoid the crash using [NSURL URLWithString:Url]? 2.有没有办法使用[NSURL URLWithString:Url]来避免崩溃?

Thanks 谢谢

需要使用

[NSURL fileURLWithPath:urPath]

While previous answers have given the correct technical response, I wanted to elaborate more. 虽然以前的答案给出了正确的技术响应,但我想详细说明。

The API docs for QLPreviewItem say that the URL must be a file-type URL, which the NSURL docs say means "uses the file: scheme". QLPreviewItem的API文档说URL 必须是文件类型的URL, NSURL文档说这意味着“使用file: scheme”。

You can also read a bit more from the Document Interaction Progamming guide which mentions that the QuickLook is supposed to give you more control than a UIDocumentInteractionController by letting you decide the way it's presented, but it brings with it the same assumptions that you've already got a file locally and you simply want a way to display it and (with QuickLook) print it with AirPrint. 您还可以从Document Interaction Progamming指南中阅读更多内容,该指南提到QuickLook应该通过让您决定它的呈现方式来为您提供比UIDocumentInteractionController更多的控制,但它带来了与您已经相同的假设在本地获得一个文件 ,您只需要一种方法来显示它,并(使用QuickLook)使用AirPrint打印它。

In your case, it may make most sense to download the file to the app's Caches directory, then open the QL preview -- it's already being downloaded by the preview view anyway , so you might as well grab it so that it can be printed too. 在您的情况下,最好将文件下载到应用程序的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"]];

Now take your original URL and download the contents and save them at cacheURL . 现在,获取原始URL并下载内容并将其保存在cacheURL If you use ASIHTTPRequest , it looks like this: 如果您使用ASIHTTPRequest ,它看起来像这样:

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

And then you use the file URL in your QuickLook view... 然后在QuickLook视图中使用文件URL ...

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

In this scenario, each new PDF that you look at from your NAS overwrites the previous one (same name, same directory), so you limit your disk usage. 在这种情况下,您从NAS查看的每个新PDF都会覆盖前一个(相同名称,相同目录),因此您可以限制磁盘使用量。 Obviously, there's lots of ways to handle that problem, so choose one that's appropriate for you. 显然,有很多方法可以解决这个问题,所以选择一个适合你的方法。 The key, though, is to download the file locally and keep it around at least until the QL view's been dismissed. 但关键是要在本地下载文件并至少保留它,直到QL视图被解除为止。

Is there any way to avoid the crash using [NSURL URLWithString:Url]? 有没有办法使用[NSURL URLWithString:Url]来避免崩溃?

Download the file to your local filesystem first. 首先将文件下载到本地文件系统。

I was able to show PDF files using HTTP scheme. 我能够使用HTTP方案显示PDF文件。 The App crashes when a user presses the print button. 当用户按下打印按钮时,应用程序崩溃。 So I needed a way just to show the PDF over HTTP and as a penalty needed to disable the print button. 因此,我需要一种方法来通过HTTP显示PDF并禁用打印按钮所需的惩罚。 Disable buttons in a subclass in viewDidAppear Here is my code 在viewDidAppear中禁用子类中的按钮这是我的代码

@interface PreviewController : QLPreviewController
@end

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

and used as code: 并用作代码:

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

By the way your statement 顺便提一下你的陈述
NSMutableString*Url = [[NSMutableString alloc] initWithFormat:@"http://10.30.24.21/Documents/abc.doc]; has a typo-error. You missed a " to indicate the end of the url string NSMutableString*Url = [[NSMutableString alloc] initWithFormat:@"http://10.30.24.21/Documents/abc.doc];有拼写错误。你错过了”表示url字符串的结尾

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM