Миграция из незашифрованной области в зашифрованную область - PullRequest
0 голосов
/ 29 ноября 2018

Я пытаюсь перейти с незашифрованной области на зашифрованную, но я не знаю, как и где использовать Realm().writeCopy(toFile: url, encryptionKey: key).или даже если есть другой способ сделать это.

Спасибо.

1 Ответ

0 голосов
/ 04 декабря 2018

Я нашел способ сделать это, вы можете найти его ниже:

private static var realm: Realm! {

    // Get the encryptionKey
    var realmKey = Keychain.realmKey
    if realmKey == nil {
        var key = Data(count: 64)

        key.withUnsafeMutableBytes { (bytes) -> Void in
            _ = SecRandomCopyBytes(kSecRandomDefault, 64, bytes)
        }
        realmKey = key
        Keychain.realmKey = realmKey
    }


    // Check if the user has the unencrypted Realm
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    let fileManager = FileManager.default
    let unencryptedRealmPath = "\(documentDirectory)/default.realm"
    let encryptedPath = "\(documentDirectory)/default_new.realm"
    let isUnencryptedRealmExsist = fileManager.fileExists(atPath: unencryptedRealmPath)
    let isEncryptedRealmExsist = fileManager.fileExists(atPath: encryptedPath)


    if isUnencryptedRealmExsist && !isEncryptedRealmExsist {
        let unencryptedRealm = try! Realm(configuration: Realm.Configuration(schemaVersion: 7))
        // if the user has unencrypted Realm write a copy to new path
        try? unencryptedRealm.writeCopy(toFile: URL(fileURLWithPath: encryptedPath), encryptionKey: realmKey)
    }

    // read from the new encrypted Realm path
    let configuration = Realm.Configuration(fileURL: URL(fileURLWithPath: encryptedPath), encryptionKey: realmKey, schemaVersion: 7, migrationBlock: { migration, oldSchemaVersion in })

    return try! Realm(configuration: configuration)
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...