我创建了一个 Core Data 应用程序,我想将 file.sqlite
保存到与默认目录不同的目录中。我遇到了这个SO thread ,然后发布this SO question ,并且该项目似乎正在编译,但我的 file.sqlite
没有保存在我指定的位置。我创建了一个方法,并向 AppDelegate.m
文件添加了一些代码,但 storeURL
变量仍然将 file.sqlite
保存在默认“文档目录。如何调用在 AppDelegate.m
文件中创建的方法来指定 storeURL
变量的正确位置?
我应该改变吗
NSURL *storeURL = [NSURL fileURLWithPath: [docPath stringByAppendingPathComponent:@"Accounts.sqlite"]];
至
NSURL *storeURL = [NSURL fileURLWithPath: [documentsDirectoryPath stringByAppendingPathComponent:@"Accounts.sqlite"]];
这也会破坏 iOS 模拟器的支持吗?
最佳答案
我通过保留模拟器的默认文档路径,然后为设备设置不同的文档路径解决了这个问题。以下代码位于我的 AppDelegate.m 文件
// add conditional code for simulator and iDevice
#if TARGET_IPHONE_SIMULATOR
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [documentPaths objectAtIndex:0];
NSURL *storeURL = [NSURL fileURLWithPath: [docPath stringByAppendingPathComponent:@"Accounts.sqlite"]];
#else
// jailbroken path - /var/mobile/Library/KegCop/
NSString *docPath = self.documentsDirectoryPath;
NSURL *storeURL = [NSURL fileURLWithPath: [docPath stringByAppendingPathComponent:@"Accounts.sqlite"]];
#endif
关于ios - 越狱 - 重新定位 Core Data 应用程序的 "Documents"目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11836775/