2011-05-14 56 views
0

我在我的iPhone应用程序中设置了UIFileSharingEnabled。我想这样做,以便用户可以访问由Core Data管理的database.sqlite文件。这样,他们可以在多个iPhone /触摸/ iPad之间拖放它,并将其用作穷人的同步。密码保护/锁定sqlite数据库在UIFileSharingEnabled应用程序之外打开?

但是,我不想让他们打开sqlite文件,并a)在数据库中进行分散操作,b)对我的数据模型进行逆向工程。

有谁知道密码保护或锁定数据库的方法,以便用户无法在应用程序之外打开它吗?

回答

0

您可以过滤出对应用程序可见的文件,但不会显示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; 
} 
+0

我希望我的sqlite数据库可以访问,以便有人可以将它从iTunes中拖出来作为备份,但无法使用sqlite编辑器打开它并使用它。 – RyeMAC3 2012-10-02 12:26:56

+0

即使文件放置在“库”目录中(这也是应用程序支持的地方)。它可以使用一些第三方应用程序访问。 – Tricertops 2013-02-21 21:41:22

0

也许你可以使用一些ZIP或RAR库,因为它们支持密码保护。然后,当您的应用移动到后台时解压缩sqlite文件,并在前台解压时解压缩。