您可以過濾出對應用程序可見的文件,但不會顯示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