Hide underlying sqlite data file when enabling iTunes file sharing

I use iTunes file sharing in my application and you need to put the sqlite Core Data database in a different place so that users do not play with it. I read a previous SO post on the best way to hide the sqlite file that uses Core Data.

There seems to be conflicting opinions as to whether to host the database in the library / Preferences or in a directory named .data , but I think I agree that the best approach is to use the .data directory.

There is currently a -applicationDocumentsDirectory method that was provided by the Core Data template code:

 - (NSString *)applicationDocumentsDirectory { return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; } 

I would like to implement a function called applicationHiddenDocumentsDirectory that will give me access to the ".data" subdirectory, but I don't know enough about Objective-C or Cocoa / Foundation frames to access the directory.

Can someone help me implement this method?

Thanks!

== Rowan ==

+6
objective-c iphone cocoa core-data
source share
2 answers

How do you like it? You must add the appropriate action if an error occurs.
EDIT: I changed this so that the database is saved in the library directory, which is backed by itunes and not displayed to the user. It was suggested by Apple Q & A

 - (NSString *)applicationHiddenDocumentsDirectory { // NSString *path = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@".data"]; NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject]; NSString *path = [libraryPath stringByAppendingPathComponent:@"Private Documents"]; BOOL isDirectory = NO; if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]) { if (isDirectory) return path; else { // Handle error. ".data" is a file which should not be there... [NSException raise:@".data exists, and is a file" format:@"Path: %@", path]; // NSError *error = nil; // if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error]) { // [NSException raise:@"could not remove file" format:@"Path: %@", path]; // } } } NSError *error = nil; if (![[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]) { // Handle error. [NSException raise:@"Failed creating directory" format:@"[%@], %@", path, error]; } return path; } 
+14
source share

Since the new CoreData templates return NSURL objects and not the NSString path, here is my updated version of the above code:

 - (NSURL *)applicationHiddenDocumentsDirectory { // NSString *path = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@".data"]; NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject]; NSString *path = [libraryPath stringByAppendingPathComponent:@"Private Documents"]; NSURL *pathURL = [NSURL fileURLWithPath:path]; BOOL isDirectory = NO; if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]) { if (isDirectory) { return pathURL; } else { // Handle error. ".data" is a file which should not be there... [NSException raise:@"'Private Documents' exists, and is a file" format:@"Path: %@", path]; // NSError *error = nil; // if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error]) { // [NSException raise:@"could not remove file" format:@"Path: %@", path]; // } } } NSError *error = nil; if (![[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]) { // Handle error. [NSException raise:@"Failed creating directory" format:@"[%@], %@", path, error]; } return pathURL; } 
+5
source share

All Articles