Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement Encrypted Core Data in Existing project with Swift 5.3.2 #336

Open
ArulkumarMS opened this issue Jan 13, 2021 · 1 comment

Comments

@ArulkumarMS
Copy link

I have a project built with Xcode 12.2, Swift 5.3.2, and Core Data. Now I need to secure my core data with this library. I have already done lightweight migration for the project. How do I configure an existing project with this library? Please help me out.
Implementation as follows,
I’m always getting crash on container.loadPersistentStores with EXEC_BAD_ACCESS
lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let url = self.applicationDocumentsDirectory.appendingPathComponent(“MyTask.sqlite")
let container = NSPersistentContainer(name: “MyTask”)
do {
let options = [
EncryptedStorePassphraseKey : “xxxxxxxxxxx”,
EncryptedStore.optionFileManager()! : EncryptedStoreFileManager.default()!
] as [String : Any]
let description = try EncryptedStore.makeDescription(options: options, configuration: nil)

        // Create Persistent Store Description
        let persistentStoreDescription = NSPersistentStoreDescription(url: url)
        // Configure Persistent Store Description
        persistentStoreDescription.type = EncryptedStoreType
        //persistentStoreDescription.setOption(NSString("xxxxxxxxxxx"), forKey: NSPersistentStoreFileProtectionKey)
        persistentStoreDescription.shouldMigrateStoreAutomatically = true
        persistentStoreDescription.shouldInferMappingModelAutomatically = true
        container.persistentStoreDescriptions = [persistentStoreDescription]
    }
    catch {
        NSLog("Could not initialize encrypted database storage: " + error.localizedDescription)
    }
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            // Replace this implementation with code to handle the error appropriately.
            // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

            /*
             Typical reasons for an error here include:
             * The parent directory does not exist, cannot be created, or disallows writing.
             * The persistent store is not accessible, due to permissions or data protection when the device is locked.
             * The device is out of space.
             * The store could not be migrated to the current model version.
             Check the error message to determine what the actual problem was.
             */
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    return container
}()
@hamada147
Copy link

hamada147 commented Apr 20, 2021

I don't know if this would solve the issue for you but here it is

lazy var persistentContainer: NSPersistentContainer = {
        /*
         The persistent container for the application. This implementation
         creates and returns a container, having loaded the store for the
         application to it. This property is optional since there are legitimate
         error conditions that could cause the creation of the store to fail.
         */
        let container = NSPersistentContainer(name: "APP_NAME_HERE")
        do {
            let configuration = EncryptedStoreFileManagerConfiguration(options: [EncryptedStoreFileManagerConfiguration.optionBundle(): Bundle.main])
            let fileManager = EncryptedStoreFileManager(configuration: configuration)!
            let options: [String: Any] = [
                EncryptedStore.optionFileManager(): fileManager,
                EncryptedStorePassphraseKey: "123"
            ]
            container.persistentStoreDescriptions = [
                try EncryptedStore.makeDescription(options: options, configuration: "local")
            ]
        } catch {
            print("Core data not encrypted")
        }
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                
                /*
                 Typical reasons for an error here include:
                 * The parent directory does not exist, cannot be created, or disallows writing.
                 * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                 * The device is out of space.
                 * The store could not be migrated to the current model version.
                 Check the error message to determine what the actual problem was.
                 */
                fatalError("Unresolved error \(error), \(error.userInfo), desc \(storeDescription)")
            }
        })
        return container
    }()

Another implementation

lazy var persistentContainer: NSPersistentContainer = {
        /*
         The persistent container for the application. This implementation
         creates and returns a container, having loaded the store for the
         application to it. This property is optional since there are legitimate
         error conditions that could cause the creation of the store to fail.
         */
        let container = NSPersistentContainer(name: "YallaSuperApp")
        do {
            let options: [AnyHashable: Any] = [
                NSPersistentStoreFileProtectionKey: FileProtectionType.complete,
                EncryptedStorePassphraseKey: "123456"
            ]
            let store = try container.persistentStoreCoordinator.addPersistentStore(ofType: EncryptedStoreType, configurationName: nil, at: container.persistentStoreDescriptions[0].url, options: options)
        } catch {
            print("Core data not encrypted \(error.localizedDescription)")
        }
    return container
}()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants