gpt4 book ai didi

ios - 从未加密 Realm 迁移到加密 Realm

转载 作者:搜寻专家 更新时间:2023-10-31 22:53:42 24 4
gpt4 key购买 nike

我正在尝试从未加密 Realm 迁移到加密 Realm ,但我不知道如何以及在何处使用 Realm().writeCopy(toFile: url, encryptionKey: key)。甚至还有其他方法。

谢谢。

最佳答案

我找到了一种方法,您可以在下面找到它:

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

关于ios - 从未加密 Realm 迁移到加密 Realm ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53539941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com