I have UIFileSharingEnabled set in my iPhone app. I wanted to do this so the user could have access to the database.sqlite file managed by Core Data. This way, they could drag and drop it between their multiple iPh开发者_高级运维ones/touchs/iPads and use it as sort of an poor-man's sync.
However, I don't want them opening the sqlite file and a) mucking around the database and b) reverse engineering my data model.
Does anyone know a way to password protect or lock out the database so that the user can't open it outside the app?
You can filter out the files that you make visible to the app, but not what is visible to iTunes FS.
When I added my iTunes File Sharing support to my app, I wrote code to silently move the DB file to a new directory that would not be visible to iTFS. The code below moves the sqlite DB file from the Documents directory to the Application Support directory which is not visible to iTFS.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
//
// Move th DB file to a different directory because of the file sharing
// feature.
//
NSString *oldDBFile = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"db.sqlite"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *newDBFile;
NSString *dbpath;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSError *error;
NSURL *storeUrl;
BOOL dir;
dbpath = ([paths count] > 0 ? [paths objectAtIndex:0] : @"");
newDBFile = [dbpath stringByAppendingPathComponent:@"db.sqlite"];
if ([dbpath length] &&
[fileMgr fileExistsAtPath:dbpath
isDirectory:&dir] == NO) {
if ([fileMgr createDirectoryAtPath:dbpath
withIntermediateDirectories:YES
attributes:nil error:&error] == NO) {
NSLog(@"Cannot create %@: %@ %@",
dbpath,
[error localizedDescription],
[error userInfo]);
}
}
if ([fileMgr fileExistsAtPath:oldDBFile]) {
if ([fileMgr moveItemAtPath:oldDBFile
toPath:newDBFile
error:&error] != YES) {
NSLog(@"Error moving DB: %@: %@", [error localizedDescription], [error userInfo]);
storeUrl = [NSURL fileURLWithPath:oldDBFile];
} else {
storeUrl = [NSURL fileURLWithPath:newDBFile];
}
} else {
storeUrl = [NSURL fileURLWithPath:newDBFile];
}
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:
[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeUrl
options:options
error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
// Handle the error.
}
return persistentStoreCoordinator;
}
Maybe you can use some ZIP or RAR library as they have support for password protection. Then zip the sqlite file when your app moves to background and unzip when comes to foreground.
精彩评论