根据 Apple 文档,当用户为设备设置有效密码时,数据保护会自动启用。系统在幕后加密和解密您的内容。这些过程是自动的并且是硬件加速的。none但我们仍然可以通过使用、complete和以编程方式设置文件保护级别completeUnlessOpen选项completeUntilFirstUserAuthentication
let options = [NSMigratePersistentStoresAutomaticallyOption:true,
NSInferMappingModelAutomaticallyOption:true,
NSPersistentStoreFileProtectionKey: FileProtectionType.complete] as [String : Any]
try persistantStoreCoordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: storeURL, options: options)
Run Code Online (Sandbox Code Playgroud)
请参阅此链接以了解有关不同类型选项的更多信息。
您找到了正确的位置,您必须打开目标功能窗格中的数据保护开关,以表明您要使用数据保护。根据苹果的文档,这应该足够了:
默认的保护级别是完全保护,其中文件被加密并且在设备锁定时无法访问。您可以通过编程方式设置应用程序创建的文件的保护级别[...]
它指出您可以通过编程方式设置保护级别。如果你想这样做(我仍然这样做,为了保存;),你应该在创建 persistenceStoreCoordinator 时使用适当的选项:
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
@YES, NSMigratePersistentStoresAutomaticallyOption,
@YES, NSInferMappingModelAutomaticallyOption,
NSFileProtectionComplete, NSPersistentStoreFileProtectionKey, // <-- HERE
nil];
...
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
...
}
Run Code Online (Sandbox Code Playgroud)
NSFileProtectionComplete方法
该文件以加密格式存储在磁盘上,在设备锁定或启动时无法读取或写入。
您还可以使用NSFileProtectionCompleteUnlessOpen,请参阅 Xcode 快速帮助以了解差异。
| 归档时间: |
|
| 查看次数: |
2836 次 |
| 最近记录: |