3

Is it possible to configure the level of protection of the SQLite file generated for the Core Data?

I need to use the NSFileProtectionComplete level on it.

Any ideas?

iHunter
  • 6,205
  • 3
  • 38
  • 56
  • Here is a question (http://stackoverflow.com/questions/18365375/ios-magical-record-sqlcipher) on sqlcipher and magical record. Maybe this will provide an adequate solution – casademora Aug 27 '13 at 15:47

1 Answers1

3

Look for the line where you do addPersistentStoreWithType:configuration:URL:options:

NSURL *storeURL = ...;

NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:...];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                              configuration:nil
                                                        URL:storeURL
                                                    options:nil
                                                      error:&error])
{
    NSLog(@"Add persistent store failed: %@", error);
}

Then add:

NSDictionary *attributes = @{NSFileProtectionKey: NSFileProtectionComplete};
if (![[NSFileManager defaultManager] setAttributes:attributes
                                      ofItemAtPath:path
                                             error:&error]) {
    NSLog(@"File protection failed: %@", error);
}

Be aware that you can't use the database in the background, consider using NSFileProtectionCompleteUnlessOpen:

  • NSFileProtectionComplete: The file is stored in an encrypted format on disk and cannot be read from or written to while the device is locked or booting.
  • NSFileProtectionCompleteUnlessOpen: The file is stored in an encrypted format on disk. Files can be created while the device is locked, but once closed, cannot be opened again until the device is unlocked. If the file is opened when unlocked, you may continue to access the file normally, even if the user locks the device. There is a small performance penalty when the file is created and opened, though not when being written to or read from. This can be mitigated by changing the file protection to NSFileProtectionComplete when the device is unlocked.
eik
  • 2,104
  • 12
  • 15
  • Note that it's even easier, and recommended, to just turn on data protection for the entire app (provided you can adapt to the backgrounding issues): http://stackoverflow.com/questions/18326225/fmdb-and-encryption/18414100#18414100 – Rob Napier Aug 27 '13 at 18:52
  • Thanks! Although I'm using MagicalRecord, this helped me! :) – Pedro Milanez Sep 04 '13 at 20:06