Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

iphone

 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.

I have two questions, 1. Can we disable the menu action button? 2. Is there any way to avoid the crash using [NSURL URLWithString:Url]?

Thanks

like image 963
Kapil Gouthaman Avatar asked Jan 11 '11 09:01

Kapil Gouthaman


3 Answers

Need to use

[NSURL fileURLWithPath:urPath]
like image 170
Alex Avatar answered Nov 15 '22 13:11

Alex


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

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.

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.

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. If you use ASIHTTPRequest, it looks like this:

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

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

like image 26
Tim Shadel Avatar answered Nov 15 '22 13:11

Tim Shadel


Is there any way to avoid the crash using [NSURL URLWithString:Url]?

Download the file to your local filesystem first.

like image 37
Matthias Bauch Avatar answered Nov 15 '22 13:11

Matthias Bauch