如何使用 Swift 在 iOS 上正确加密/解密 Core Data?

发布于 2025-01-17 10:26:38 字数 901 浏览 2 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文