核心数据和数据保护(CoreData & Data Protection)

编程入门 行业动态 更新时间:2024-10-25 06:28:41
核心数据和数据保护(CoreData & Data Protection)

所以我正在开发一个应用程序,它在本地存储用户信息和CoreData框架。 信息可能很敏感,所以我在想如何保护存储在数据库中的信息。 在Xcode仪表板的功能选项卡下,我找到了这个数据保护开关:

谁知道这是如何工作的? 如果我打开开关,Xcode会自动编码我的CoreData文件吗? 或者如何在我的CoreData文件上实现这种保护? 感谢您的时间和耐心。 谢谢!

So I am working on a app which stores user information with CoreData framework locally. The information can be sensitive, so I am thinking how to protect the information stored to the data base. In Xcode dashboard, under the capabilities tab, I found this Data Protection switch:

Anyone knows how this works? If I turn on the switch will Xcode encode my CoreData files automatically? Or how to implement this protection on my CoreData files? Appreciate your time and patient. Thank you!

最满意答案

您找到了正确的位置,您必须打开目标功能窗格中的数据保护开关,以表示您要使用数据保护。 根据Apple的文档 ,这应该足够了:

默认保护级别是完全保护,其中文件被加密并且在设备被锁定时不可访问。 您可以以编程方式设置应用程序创建的文件的保护级别[...]

它声明您可以以编程方式设置保护级别。 如果你想这样做(我仍然这样做,要保存;),你应该在创建persistentStoreCoordinator时使用适当的选项:

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: @YES, NSMigratePersistentStoresAutomaticallyOption, @YES, NSInferMappingModelAutomaticallyOption, NSPersistentStoreFileProtectionKey, NSFileProtectionComplete, // <-- HERE nil]; ... __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) { ... }

NSFileProtectionComplete意味着

该文件以加密格式存储在磁盘上,在设备锁定或引导时无法读取或写入。

您也可以使用NSFileProtectionCompleteUnlessOpen ,有关差异,请参阅Xcode快速帮助。

You found the right spot, you have to turn on the Data Protection switch in your target's capabilities pane to signal that you want to use data protection. According to Apple's documentation, this should suffice:

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 [...]

It states you can set the level of protection programmatically. If you want to do that (I still do that, to be save ;), you should use the appropriate option when creating the persistentStoreCoordinator:

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: @YES, NSMigratePersistentStoresAutomaticallyOption, @YES, NSInferMappingModelAutomaticallyOption, NSPersistentStoreFileProtectionKey, NSFileProtectionComplete, // <-- HERE nil]; ... __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) { ... }

NSFileProtectionComplete means

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.

You could also use NSFileProtectionCompleteUnlessOpen, see the Xcode Quick Help for differences.

更多推荐

本文发布于:2023-07-14 20:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107222.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   核心   CoreData   Data   Protection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!