Backup

🔌 Retrieve backup

Suppose the user is already enrolled and you have your backup system in place. To retrieve the Keyless backup data and backup key invoke the Keyless.shared.retrieveDataForBackup method. You can store the backup data and backup key to let users restore their account with Keyless.

The backup data (type: String) and backupKey (type: String) are required to restore an account with Keyless. Save the Keyless backup in a secure data store.

func retrieveKeylessBackup() {
do {
let backup = try Keyless.shared.retrieveDataForBackup()
let backupData = backup.backupData
let backupKey = backup.backupKey
// Handle your backup system logic
} catch {
// Handle error and display an error to the user.
}
}

🔌 Restore from backup

Suppose your backup system provided the Keyless backupData and backupKey that was saved when the current user was enrolled. You can invoke the Keyless.shared.enrollFromBackup(backupData: String, backupKey: String) method to let the user restore their Keyless data and enroll from backup.

func enrollFromBackup(backupKey: String, backupData: String) {
DispatchQueue.main.async { [weak self] in
// Start loading indicator
}
do {
try Keyless.shared.enrollFromBackup(backupData: backupData, backupKey: backupKey)
} catch {
// Handle error
}
}

As with Keyless.shared.enrollCustomToken when Keyless.shared.enrollFromBackup finishes, the enrollmentDidFinish: keylessID: enrollmentResponse: error: delegate method gets called. In case of errors, prompt the user to try again.

If the process was successful, stop the loading indicator and navigate to the next screen of your application (e.g. success or main view).

func enrollmentDidFinish(keylessID: String?, enrollmentResponse: String?, error: KeylessError?) {
guard error == nil else {
print("enrollmentDidFail", error!)
DispatchQueue.main.async {
// Stop the loading indicator
// Display an error to the user.
}
return
}
// Stop the loading indicator
// Display success to the user
}