Skip to content

Instantly share code, notes, and snippets.

@kaspermunck
Created August 10, 2016 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaspermunck/9b1dcbf07773b22e109eefd552200e00 to your computer and use it in GitHub Desktop.
Save kaspermunck/9b1dcbf07773b22e109eefd552200e00 to your computer and use it in GitHub Desktop.
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