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?
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?
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.