如何快速使用EncryptedCoreData进行加密? [英] How to encrypt using EncryptedCoreData with swift?

查看:140
本文介绍了如何快速使用EncryptedCoreData进行加密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在弄清楚如何加密我的sqlite数据库时遇到了一些麻烦.我正在使用核心数据和以下项目:

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

我不知道是怎么使用这个项目来加密我的数据库的.我已经安装了该项目,并且可以导入库EncryptedCoreData.但是,我找不到任何有关迅捷的实用示例的信息.在我的appdelegate中,我有以下代码

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?

推荐答案

我将Objective-C转换为Swift,并且可以正常工作,我只添加了以下几行

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)")
        }
})

这篇关于如何快速使用EncryptedCoreData进行加密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆