Display encrypted file using QuickLook framework or UiDocumentInteractionController

后端 未结 4 967
迷失自我
迷失自我 2021-02-09 13:34

I have an encrypted word/excel/pdf file locally stored which I need to preview in my iPad app. I understand that QLPreviewController or UiDocumentInteractionController could be

4条回答
  •  清歌不尽
    2021-02-09 14:19

    Since you are using Quick Look, your options are limited. You must give Quick Look an NSURL, which means it must be on the file system (or the Internet). Fortunately, this shouldn't be much of a problem. iOS devices use hardware-level encryption. When your file is encrypted, only your app has the key to decrypt it. So, your file will still be encrypted, but it will also be readable by your app and only your app.

    Here's what you do:

    1. Decrypt your file into an NSData object, which you've already done.

    2. Write the file to a location that will not get uploaded to iCloud nor backed up by iTunes. The tmp directory is probably the best choice. The code looks something like this:

      NSData * data = // Your decrypted file data.
      NSString * fileName = // Whatever you want to name your file.
      NSString * path = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
      NSURL * url = [NSURL URLWithString:path];
      NSError * error = nil;
      
      BOOL success = [data writeToURL:url
                              options:NSDataWritingFileProtectionComplete
                                error:&error];
      if (success) {
          // Give the URL to Quick Look.
      }
      else {
          // An error happened. See the 'error' object for the details.
      }
      

      At this point you have an NSURL which you can use with Quick Look. Don't forget to delete the decrypted file when you are done with it.

    There are a few things to note about on-disk encryption:

    1. It is only supported on iOS 4.0+.

    2. It may not work on "older" devices.

    3. The user must have an active passcode.

    4. If you use NSDataWritingFileProtectionComplete, the file is not accessible while the device is locked. If you need to access the file while the app is locked, then you should use NSDataWritingFileProtectionCompleteUnlessOpen or NSFileProtectionCompleteUntilFirstUserAuthentication instead. This will still give you great protection, even if the device is stolen and jailbroken. Be aware, though, that these encryption options are only available on iOS 5.0+

    For more details for on-disk encryption, check out the iOS App Programming Guide

提交回复
热议问题