Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Creating encrypted Realm and attempting to catch any exception thrown.
var configuration = Realm.Configuration.defaultConfiguration
let encryptionKey = NSMutableData(length: 64)!
SecRandomCopyBytes(kSecRandomDefault, encryptionKey.length, UnsafeMutablePointer<UInt8>(encryptionKey.mutableBytes))
configuration.encryptionKey = encryptionKey
let filename = "private.realm"
let fileURL = configuration.fileURL?.URLByAppendingPathComponent(filename)
configuration.fileURL = fileURL
var realm: Realm!
do {
realm = try Realm(configuration: configuration)
catch let error {
// handle exception
// Throws `libc++abi.dylib: terminating with uncaught exception of type realm::RealmFileException: Unable to open a realm at path '.../private.realm': Realm file decryption failed.` when wrong encryption key is used. Expected it would be caught.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.