核心数据和数据保护
CoreData & Data Protection
所以我正在开发一个在本地使用 CoreData
框架存储用户信息的应用程序。这些信息可能很敏感,所以我在考虑如何保护存储在数据库中的信息。在 Xcode 仪表板的功能选项卡下,我找到了这个数据保护开关:
有人知道这是怎么回事吗?如果我打开开关,Xcode 会自动编码我的 CoreData
文件吗?或者如何对我的 CoreData
文件实施这种保护?
感谢您的时间和耐心。谢谢!
您找到了正确的位置,您必须打开目标功能面板中的数据保护开关,以表明您想要使用数据保护。根据 Apple's documentation,这应该足够了:
The default level of protection is complete protection, in which files
are encrypted and inaccessible when the device is locked. You can
programmatically set the level of protection for files created by your
app [...]
它声明您可以以编程方式设置保护级别。如果你想这样做(我仍然这样做,为了省事;),你应该在创建 persistentStoreCoordinator 时使用适当的选项:
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]) {
...
}
NSFileProtectionComplete
表示
The file is stored in an encrypted format on disk and cannot be read
from or written to while the device is locked or booting.
您也可以使用 NSFileProtectionCompleteUnlessOpen
,请参阅 Xcode 快速帮助了解不同之处。
根据 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)
请参阅此 link 以了解有关不同类型选项的更多信息。
所以我正在开发一个在本地使用 CoreData
框架存储用户信息的应用程序。这些信息可能很敏感,所以我在考虑如何保护存储在数据库中的信息。在 Xcode 仪表板的功能选项卡下,我找到了这个数据保护开关:
有人知道这是怎么回事吗?如果我打开开关,Xcode 会自动编码我的 CoreData
文件吗?或者如何对我的 CoreData
文件实施这种保护?
感谢您的时间和耐心。谢谢!
您找到了正确的位置,您必须打开目标功能面板中的数据保护开关,以表明您想要使用数据保护。根据 Apple's documentation,这应该足够了:
The default level of protection is complete protection, in which files are encrypted and inaccessible when the device is locked. You can programmatically set the level of protection for files created by your app [...]
它声明您可以以编程方式设置保护级别。如果你想这样做(我仍然这样做,为了省事;),你应该在创建 persistentStoreCoordinator 时使用适当的选项:
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]) {
...
}
NSFileProtectionComplete
表示
The file is stored in an encrypted format on disk and cannot be read from or written to while the device is locked or booting.
您也可以使用 NSFileProtectionCompleteUnlessOpen
,请参阅 Xcode 快速帮助了解不同之处。
根据 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)
请参阅此 link 以了解有关不同类型选项的更多信息。