How to encrypt using EncryptedCoreData with swift?

自闭症网瘾萝莉.ら 提交于 2020-01-04 14:05:40

问题


I've been having some trouble figuring out how to encrypt my sqlite database. I'm using core data and this following project:

https://cocoapods.org/?q=EncryptedCoreData

What I can't figure out is how I am suppose to use this project to encrypt my database. I've already install the project and I can import the library EncryptedCoreData. However I don't find any information regarding a pratical example with swift. In my appdelegate I have the following code

import UIKit
//import CoreData
//import SQLCipher
import EncryptedCoreData


lazy var persistentContainer: NSPersistentContainer = {
    // my attempt to initialize the container
    let modelURL = Bundle.main.url(forResource: "DbModel", withExtension: "momd")!
    var coordinator = NSPersistentStoreCoordinator.init(managedObjectModel: NSManagedObjectModel(contentsOf: modelURL)!)

    //originaly its
    let container = NSPersistentContainer(name: "DbModel")
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    return container
}()

Can someone provide an example on how I'am suppose to initialize the container?


回答1:


I translated the Objective-C to Swift and it worked, I just added this lines

let container = NSPersistentContainer(name: "DbModel")
// Begin of my code
let cOpts : NSDictionary = [
            EncryptedStore.optionPassphraseKey() : "123deOliveira4", //your Key
            EncryptedStore.optionFileManager() : EncryptedStoreFileManager.default()
        ]
let desc = try! EncryptedStore.makeDescription(options: cOpts as! [AnyHashable : Any], configuration: nil)
container.persistentStoreDescriptions = [desc]
//End
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
})


来源:https://stackoverflow.com/questions/45032046/how-to-encrypt-using-encryptedcoredata-with-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!