Reputation: 649
I want to use TouchID authenticate my app, authentication worked successfully. If TouchID does not match, then the Try Again alert opens, and in that alert is the Enter Password option. If the user selects that, the system passcode authentication should display, but how can I do that?
Here share my code:
func touchIDAuthentication() {
let context = LAContext() //1
var error:NSError?
guard context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) else {
showAlertViewIfNoBiometricSensorHasBeenDetected()
return
}
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &errorPointer) {
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason, reply: { (success, error) in
if success {
DispatchQueue.main.async {
print("Authentication was successful")
}
}else {
DispatchQueue.main.async {
self.displayErrorMessage(error: error as! LAError )
print("Authentication was error")
}
}
})
}else {
self.showAlertWith(title: "Error", message: (errorPointer?.localizedDescription)!)
}
}
func displayErrorMessage(error:LAError) {
var message = ""
switch error.code {
case LAError.authenticationFailed:
message = "Authentication Failed."
break
case LAError.userCancel:
message = "User Cancelled."
break
case LAError.userFallback:
message = "Fallback authentication mechanism selected."
break
case LAError.touchIDNotEnrolled:
message = "Touch ID is not enrolled."
case LAError.passcodeNotSet:
message = "Passcode is not set on the device."
break
case LAError.systemCancel:
message = "System Cancelled."
break
default:
message = error.localizedDescription
}
self.showAlertWith(title: "Authentication Failed", message: message)
}
How to show this screen if enter the passcode it move into my app. How achieve this help me. Thanks advance.
Upvotes: 13
Views: 13610
Reputation: 79646
Replace LAPolicy
policy enum value deviceOwnerAuthenticationWithBiometrics
with deviceOwnerAuthentication
Note: If user has enable
biometric
(face id or touch id) authentication, then device will ask first for biometric authentication and if user choose fall back authentication, then onlydeviceOwnerAuthentication
will show passcode screen.
Try this and see:
func touchIDAuthentication() {
let context = LAContext()
var error:NSError?
// edit line - deviceOwnerAuthentication
guard context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) else {
//showAlertViewIfNoBiometricSensorHasBeenDetected()
return
}
// edit line - deviceOwnerAuthentication
if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &errorPointer) {
// edit line - deviceOwnerAuthentication
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason, reply: { (success, error) in
if success {
DispatchQueue.main.async {
print("Authentication was successful")
}
}else {
DispatchQueue.main.async {
//self.displayErrorMessage(error: error as! LAError )
print("Authentication was error")
}
}
})
}else {
// self.showAlertWith(title: "Error", message: (errorPointer?.localizedDescription)!)
}
}
Upvotes: 21
Reputation: 114836
If you use policy .deviceOwnerAuthentication
then the "Enter password" option is displayed immediately.
If you use .deviceOwnerAuthenticationWithBiometrics
, as you are, then the "Enter Password" option is only shown after the first unsuccessful biometric authentication attempt.
Regardless of how the user authenticates, your completion closure will be called.
Upvotes: 22