Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent iOS QuickLook from generating a cache

I'm using the QuickLook Framework in my iOS app to preview various files. I recently noticed that QuickLook is keeping a cache of all previewed files in my app's tmp directory. There are two issues with QuickLook caching files though:

  1. These cached files are already stored in the Documents directory. So in theory the app could double in size if the user wants to preview all of their files (which they probably will). Okay, no big deal, I can periodically clean out the cache using this code:

    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSArray *fileArray = [fileMgr contentsOfDirectoryAtPath:NSTemporaryDirectory() error:nil];
     for (NSString *filename in fileArray)  {
        [fileMgr removeItemAtPath:[NSTemporaryDirectory() stringByAppendingPathComponent:filename] error:NULL];
    }
  2. The second, and most important issue is that QuickLook seems to be creating a secondary cache in a system folder (not my app). This is an issue because users have the option to encrypt individual files in my app. If they preview a file before encrypting, it looks normal. If they encrypt and then preview afterwards, the file still appears as if it was not encrypted. I know there is not a problem with the encryption because the encrypted file stored in my app's Documents directory is encrypted and can't be read (properly) by any program. This is a major issue, as it may cause the user to believe the file was not encrypted. It is also a security issue because it means that QuickLook is caching the file elsewhere.

Here is my QuickLook code:

- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
    return 1;
 }

- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    //Get the stored user data and file name
    NSString *FileName = [defaults objectForKey:@"FileName"]; //Name of file stored in NSUserDefaults
    NSString *finalFilePath = [documentsDirectory stringByAppendingPathComponent:FileName];

    return [NSURL fileURLWithPath:finalFilePath];
}

- (IBAction)previewFile:(id)sender {
   //Setup QuickLook
   QLPreviewController *previewController = [[QLPreviewController alloc] init];
   previewController.delegate=self;
   previewController.dataSource=self;
   previewController.currentPreviewItemIndex = 0;
   [previewController reloadData];
   [self presentModalViewController:previewController animated:YES];
   [previewController.navigationItem setRightBarButtonItem:nil];
}

Does anyone know where the QuickLook cache is, or if there is one (maybe it's some kind of bug)?

How can I prevent QuickLook from caching files, or how can I reset the cache? If neither of these are possible is there an iOS 6 alternative to QuickLook (I've heard QuickLook was changed in iOS 6 so there aren't any workarounds, alternatives, etc.)?

Any solutions or ideas appreciated!

like image 861
Sam Spencer Avatar asked Apr 20 '13 15:04

Sam Spencer


1 Answers

If there isn't a way to prevent QLPreviewController from doing it's own caching, you might want to consider making your own version QLPreviewController with a UIWebView (since thats capable of viewing many file types such as PDF, docs, xls, etc.).

It's by no means an ideal solution and you won't have anywhere near the level of performance/UI quality as the QLPreviewController will provide. Yeah... it's a bit of a hacked solution, but short of a public API to clear the secondary cache... it's the only thing I can think of.

I guess it's a toss up between user friendliness + security.

Good luck!

like image 107
Jamie Chapman Avatar answered Sep 29 '22 12:09

Jamie Chapman