当iTunes文件共享启用时隐藏核心数据sqlite文件 [英] Hide Core Data sqlite file when iTunes File Sharing is Enabled
问题描述
我在我的应用程序中使用iTunes文件共享,并需要将Core Data的sqlite数据库放在别处,以便用户不会搞砸。我已阅读以前的SO帖,了解最佳方式隐藏Core Data使用的sqlite文件。
I am using iTunes file sharing in my app, and need to put Core Data's sqlite database elsewhere so that users don't fiddle with it. I have read a previous SO post regarding the best way to hide the sqlite file that Core Data uses.
对于将数据库放在Library / Preferences中还是在名为 .data
,但我认为我同意最好的方法是使用 .data
目录。
There seems to be conflicting opinions regarding whether to put the database in Library/Preferences or in a directory called .data
, but I think I agree the best approach is to use the .data
directory.
-applicationDocumentsDirectory
方法由Core Data模板代码提供:
There is currently an -applicationDocumentsDirectory
method that was provided by the Core Data template code:
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
我想实现一个函数 applicationHiddenDocumentsDirectory
,这将让我访问.data子目录,但我不知道Objective-C或Cocoa / Foundation框架访问该目录。
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 the Cocoa/Foundation frameworks to access the directory.
有人可以帮我实现这个方法吗?
Could someone help me implement this method please?
谢谢!
== Rowan ==
==Rowan==
推荐答案
如果发生错误,您必须添加相应的操作。
编辑:我改变了这样,所以数据库保存在库目录中,它由iTunes备份,用户不可见。这是由 Apple Q& A 建议的
how do you like this one? You have to add appropriate action if an error occurs.
I changed this so the database is saved in the library-directory, which is backed up by itunes and not visible to the user. This 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;
}
这篇关于当iTunes文件共享启用时隐藏核心数据sqlite文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!