2011-05-14 62 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文件,並在前臺解壓時解壓縮。