【问题标题】:Core Data Encryption with MagicalRecord使用 MagicalRecord 进行核心数据加密
【发布时间】:2013-08-27 14:59:23
【问题描述】:

是否可以配置为 Core Data 生成的 SQLite 文件的保护级别?

我需要在上面使用NSFileProtectionComplete 级别。

有什么想法吗?

【问题讨论】:

标签: ios core-data magicalrecord


【解决方案1】:

寻找你做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 来缓解。

【讨论】:

猜你喜欢
  • 2020-02-28
  • 2015-06-13
  • 2012-11-25
  • 2013-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多