iOS - how to change Documents directory

我只是一个虾纸丫 提交于 2019-12-11 03:27:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!