Reputation: 75
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?
Upvotes: 2
Views: 2172
Reputation: 503
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)")
}
})
Upvotes: 2