您可以过滤出对应用程序可见的文件,但不会显示iTunes FS可见的文件。
当我将我的iTunes文件共享支持添加到我的应用程序时,我编写了代码以静默地将数据库文件移动到iTFS不可见的新目录。下面的代码将sqlite数据库文件从Documents目录移动到iTFS不可见的应用程序支持目录。
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
//
// Move th DB file to a different directory because of the file sharing
// feature.
//
NSString *oldDBFile = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"db.sqlite"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *newDBFile;
NSString *dbpath;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSError *error;
NSURL *storeUrl;
BOOL dir;
dbpath = ([paths count] > 0 ? [paths objectAtIndex:0] : @"");
newDBFile = [dbpath stringByAppendingPathComponent:@"db.sqlite"];
if ([dbpath length] &&
[fileMgr fileExistsAtPath:dbpath
isDirectory:&dir] == NO) {
if ([fileMgr createDirectoryAtPath:dbpath
withIntermediateDirectories:YES
attributes:nil error:&error] == NO) {
NSLog(@"Cannot create %@: %@ %@",
dbpath,
[error localizedDescription],
[error userInfo]);
}
}
if ([fileMgr fileExistsAtPath:oldDBFile]) {
if ([fileMgr moveItemAtPath:oldDBFile
toPath:newDBFile
error:&error] != YES) {
NSLog(@"Error moving DB: %@: %@", [error localizedDescription], [error userInfo]);
storeUrl = [NSURL fileURLWithPath:oldDBFile];
} else {
storeUrl = [NSURL fileURLWithPath:newDBFile];
}
} else {
storeUrl = [NSURL fileURLWithPath:newDBFile];
}
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:
[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeUrl
options:options
error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
// Handle the error.
}
return persistentStoreCoordinator;
}
我希望我的sqlite数据库可以访问,以便有人可以将它从iTunes中拖出来作为备份,但无法使用sqlite编辑器打开它并使用它。 – RyeMAC3 2012-10-02 12:26:56
即使文件放置在“库”目录中(这也是应用程序支持的地方)。它可以使用一些第三方应用程序访问。 – Tricertops 2013-02-21 21:41:22