QLPreviewController: Where is the temporary file located?
-
29-10-2019 - |
Pergunta
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?
Solução
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.
Outras dicas
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];