2013-08-27 73 views
3

是否可以配置爲Core Data生成的SQLite文件的保護級別?使用MagicalRecord核心數據加密

我需要使用它的NSFileProtectionComplete級別。

任何想法?

+0

這是一個關於sqlcipher和神奇記錄的問題(http://stackoverflow.com/questions/18365375/ios-magical-record-sqlcipher)。也許這將提供一個適當的解決方案 – casademora

回答

3

查找,你做addPersistentStoreWithType:configuration:URL:options:

NSURL *storeURL = ...; 

NSError *error = nil; 
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:...]; 
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
               configuration:nil 
                 URL:storeURL 
                options:nil 
                 error:&error]) 
{ 
    NSLog(@"Add persistent store failed: %@", error); 
} 

行則附加:

NSDictionary *attributes = @{NSFileProtectionKey: NSFileProtectionComplete}; 
if (![[NSFileManager defaultManager] setAttributes:attributes 
             ofItemAtPath:path 
              error:&error]) { 
    NSLog(@"File protection failed: %@", error); 
} 

要知道,你不能在後臺使用的數據庫,可以考慮使用NSFileProtectionCompleteUnlessOpen:

  • NSFileProtectionComplete: 該文件存儲在磁盤上的加密格式,在設備鎖定或引導時無法讀取或寫入。
  • NSFileProtectionCompleteUnlessOpen: 該文件以加密格式存儲在磁盤上。文件可以在設備鎖定時創建,但一旦關閉,在設備解鎖之前不能再次打開。如果文件在解鎖時打開,即使用戶鎖定設備,也可以繼續正常訪問文件。當文件被創建並打開時,性能損失很小,但在寫入或讀取時不會受到影響。這可以通過在設備解鎖時將文件保護更改爲NSFileProtectionComplete來緩解。
+0

請注意,爲整個應用打開數據保護(只要您可以適應後臺問題)就更簡單了,建議您這麼做:http://stackoverflow.com/questions/18326225/ fmdb-and-encryption/18414100#18414100 –

+0

謝謝!雖然我使用MagicalRecord,但這幫助了我! :) –

+1

這是怎麼用MagicalRecord完成的? – lostintranslation