如何使用 Swift 在 iOS 上正确加密/解密 Core Data?
我是 iOS 开发新手。我有一个本地 SQLite 数据库,它使用 Core Data。我想加密这个数据库,这样当有人从 iPhone 设备检索带有数据库的文件时,就无法读取其内容。我找到了几个解决方案(包括 StackOverflow 线程)和第三方库,但它们不起作用或者不支持我正在使用的功能(例如批量删除)。我在官方文档中也找不到相关信息。您能否提供一个使用 Swift 在 iOS 上使用 Core Data 加密 SQLite 数据库的完整示例,或者给我一些提示或资源,我该如何做到这一点?我很欣赏你的回答。
我按以下方式初始化数据库:
let bundle = Bundle(for: DatabaseManager.self)
let modelUrl = bundle.url(forResource: modelName, withExtension: "momd")!
let managedObjectModel = NSManagedObjectModel(contentsOf: modelUrl)!
persistentContainer = NSPersistentContainer(name: modelName, managedObjectModel: managedObjectModel)
persistentContainer.loadPersistentStores { (storeDescription, error) in
if let error = error as NSError? {
FrameworkLogger.error("Unresolved error \(String(describing: error)), \(error.userInfo)")
}
context = persistentContainer.newBackgroundContext()
如何为此代码添加加密?
问候,
皮奥特尔
I'm new to the iOS development. I have a local SQLite database, which is using Core Data. I'd like to encrypt this database, so in the case when someone retrieves the file with the database from the iPhone device, it wouldn't be possible to read its contents. I found several solutions (including StackOverflow threads) and third-party libraries, but they don't work or they don't support functionalities I'm using (e.g. batch delete). I also could not find relevant information in the official documentation. Could you please, provide a complete example of encryption of the SQLite database with Core Data on iOS with Swift or give me some hints or resources how can I do that? I appreciate your answers.
I'm initializing database in the following way:
let bundle = Bundle(for: DatabaseManager.self)
let modelUrl = bundle.url(forResource: modelName, withExtension: "momd")!
let managedObjectModel = NSManagedObjectModel(contentsOf: modelUrl)!
persistentContainer = NSPersistentContainer(name: modelName, managedObjectModel: managedObjectModel)
persistentContainer.loadPersistentStores { (storeDescription, error) in
if let error = error as NSError? {
FrameworkLogger.error("Unresolved error \(String(describing: error)), \(error.userInfo)")
}
context = persistentContainer.newBackgroundContext()
How can I add encryption to this code?
Regards,
Piotr
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论