Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// oldDomainState - is the value of context.evaluatedPolicyDomainState after
// the user entered the passcode manually
let context = LAContext()
context.localizedCancelTitle = "Enter App's Passcode"
// First check if we have the needed hardware support.
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {
let reason = "Access secure data"
if let domainState = context.evaluatedPolicyDomainState, domainState == oldDomainState {
// Enrolment state is unchanged
// Try and authenticate the user using Touch ID / Face ID
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason ) { success, error in
if success {
// The user passed Touch ID / Face ID Verification
// Grant him access to the secure data
} else {
print(error?.localizedDescription ?? "Failed to authenticate")
// The user failed Touch ID / Face ID Verification
// Fallback to: ask for passcode. (if passcode is valid update oldDomainState)
}
}
} else {
// Enrolment state is changed
// Fallback to: ask for passcode. (if passcode is valid update oldDomainState)
}
} else {
print(error?.localizedDescription ?? "Can't evaluate policy")
// Touch ID / Face ID not enrolled or no hardware support
// Fallback to: ask for passcode. - in this case you don't need to
// update oldDomainState since canEvaluatePolicy failed
}
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.