0

I am using QLPreviewController to fetch and view a remote PDF from my server. This works very nicely, since Apple built in a nice "Loading" graphic while the file is downloading. Now, I would also like to use UIDocumentInteractionController which requires a local file. But, since QLPreviewController already downloaded the file, I would like to use that.

Is there a way to get the local path to the file that QLPreviewController downloaded?

QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.dataSource = self;
previewController.delegate = self;
previewController.currentPreviewItemIndex = 0;
[[self navigationController] pushViewController:previewController animated:YES];
[previewController release];
// Where is the file stored?
Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412

2 Answers2

4

We had a similar scenario where the file to be shown is remote. Here's what we did: We set the datasource to nil. What this does is show a loading icon. In viewDidLoad we initialize the remote request and save the file locally. Once the file has been downloaded, we set the datasource correctly and refresh the view.

 //after file has been downloaded
 [self setDataSource:self];
 [self reloadData];
singhspk
  • 2,389
  • 3
  • 23
  • 28
1

QLPreviewController isn't really designed to download files - from the documentation for QLPreviewItem:

@property (readonly) NSURL *previewItemURL;

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.

So you should really be using something else to download the file. It may work with a network resource, but it's not designed to really work with it. Not even sure how you're managing it, because I was pretty sure a QuickLookController would error out if you didn't pass a file URL. The controller is most likely downloading the file either to a temp directory, or directly to memory. Either way, it's not available for you to use.

You should download the file yourself, and then pass the Quick Look controller a file URL pointing to it. You can use a networking library like ASIHTTP if you wanted to make it easier on you.

lxt
  • 31,146
  • 5
  • 78
  • 83
  • Sorry for bringing it up after almost 2 months but I am stuck with the same problem, and I tried to use UIDocumentInteractionController, but I can only show that through modal. The QLPreviewController has a property 'currentPreviewItem' but it just returns a QLPreviewItem witch has a URL, but that is not local.. I had considered your answer but I want to interact with the toolbar while the file is loading. Any tips? – Justin Aug 22 '11 at 13:55
  • As I mentioned, you're going to want to download the file asynchronously (in the background) using either the built in APIs (like `NSURLConnection`) or a third-party library (like `ASIHTTPRequest`), and then only try and open the file once it is downloaded to the phone's memory. If you download it asynchronously you won't block user interaction. – lxt Aug 22 '11 at 14:37