问题
Currently the program I am working on uses Core Data, and stores "Account" information in a file called Accounts.sqlite
This file is stored in the Documents
directory inside the long string of characters, i.e. A89C1398-A7BE-44F1-A337-DABA9C66AF1D
So I want to save / create the file for the application in /var/mobile/Library/KegCop
I found this SO question and I made the necessary changes, deleted my app off the phone, clicked the Run
button in Xcode and the app built and ran fine. However the Accounts.sqlite
file was still in the Documents
directory.
I added / modified the follow code to the AppDelegate.m
file,
// default sqlite file path
// NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Accounts.sqlite"];
// jailbroken path - /var/mobile/Library/KegCop/
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [documentPaths objectAtIndex:0];
NSURL *storeURL = [NSURL fileURLWithPath: [docPath stringByAppendingPathComponent:@"Accounts.sqlite"]];
and then created the following method in the AppDelegate.m
file,
// set path for documents in jailbreak environment
+(NSString *)documentsDirectoryPath
{
#ifdef JAILBREAK
NSString *documentPath =@"/var/mobile/Library/KegCop";
if (![[NSFileManager defaultManager] fileExistsAtPath:documentPath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:documentPath
withIntermediateDirectories:NO
attributes:nil
error:NULL];
}
return documentPath;
#else
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [documentPaths objectAtIndex:0];
#endif
}
来源:https://stackoverflow.com/questions/11812576/ios-how-to-change-documents-directory