3

我正在使用 SQLite 数据库制作 ipad 应用程序。

我希望能够从 iTunes 添加 .sqlite 文件以自由添加 db 文件并备份 user_data。

所以,我必须将文件保存在 /Documents 文件夹中。

然后,我有一个问题。

我不想在 iTunes 中显示一些数据库文件。- (ex. like user memo db)

我应该做些什么?

4

1 回答 1

2

如果您不想向用户显示数据,您可以使用以下文件夹并从用于存储的同一位置访问数据库

//访问iOS机器的ApplicationSupport文件夹

NSString *appSupportDir = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject];

//访问应用程序的Library文件夹(即Application/appID/Library)

NSString *libraryDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];

要将数据从一个文件夹移动到另一个文件夹,请使用以下代码:

NSFileManager *mngr = [[NSFileManager alloc] init];

NSString *ExpectedFilePath=[[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"Filename.ext"];

//getting Document directory path
NSString *FilePresentAtPath=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"Filename.ext"];

 // If the database doesn't exist in our Document folder we copy it to Library (this will be executed only one time).
 if (![mngr fileExistsAtPath:ExpectedFilePath])
 {
     [mngr moveItemAtPath:FilePresentAtPath toPath:ExpectedFilePath error:NULL];
 }
[mngr release];
于 2012-05-09T14:41:52.380 回答