iOS Touch ID Authentication

  • 2021-11-02 02:58:18
  • OfStack

iOS Touch ID Authentication

iOS 8 and later devices with fingerprints can use touch ID for identity authentication, and only when the fingerprints match the entered fingerprints can the authentication be successful.

Steps

Import LocalAuthentication Framework: import LocalAuthentication Initialize the LAContext object: let context = LAContext () Call canEvaluatePolicy (_ policy: LAPolicy, error: NSErrorPointer) of the LAContext object- > Bool method If false is returned in the previous step, it means that authentication cannot be carried out and the corresponding failure operation is executed; If true is returned, call evaluatePolicy of the LAContext object (_ policy: LAPolicy, localizedReason: String, reply: @ escaping (Bool, Error? )- > Void) method to determine whether authentication is successful in reply to perform the corresponding operation (if authentication fails, you can get the error code code to see which type of error belongs to LAError. Code to perform the corresponding failed operation)

Calling the canEvaluatePolicy and evaluatePolicy methods of the LAContext object passes in values of the LAPolicy enumeration type, which are currently available in two categories: deviceOwnerAuthenticationWithBiometrics and deviceOwnerAuthentication. The first deviceOwnerAuthenticationWithBiometrics is for fingerprint authentication. The latter deviceOwnerAuthentication can only be used after iOS 9.0. Fingerprint authentication should be carried out first. If fingerprint authentication fails, authentication can be carried out by entering password.

Calling the evaluatePolicy method of the LAContext object will pop up the fingerprint authentication dialog box. The dialog box displays the reason for the authentication (String), which is the value of the localizedReason parameter. The dialog box has a Cancel button, and iOS 10.0 and later can change the word displayed by the Cancel button by setting the localizedCancelTitle value of the LAContext object. If fingerprint authentication fails, the dialog box also displays the fallback button. You can set the localizedFallbackTitle value of the LAContext object to change the word displayed by the fallback button.

Note that the reply callback of the evaluatePolicy method is not on the main thread. If you need to update UI, call the main thread to update it again.

Code Sample

The code has been uploaded to GitHub: https://github.com/Silence-GitHub/TouchIDDemo

Place an label in the controller to display the authentication return result.

Fingerprint authentication code


let context = LAContext()
context.localizedFallbackTitle = "Fall back button"
if #available(iOS 10.0, *) {
 context.localizedCancelTitle = "Cancel button"
}
var authError: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
 context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Localized reason for authentication with biometrics", reply: { (success, evaluateError) in
 // NOT in main thread
 DispatchQueue.main.async {
 if success {
 self.label.text = "Success"
 // Do something success
 } else if let error = evaluateError {
 self.label.text = error.localizedDescription
 // Deal with error
 if let code = LAError.Code(rawValue: (error as NSError).code) {
  switch code {
  case .userFallback:
  print("fall back button clicked")
  default:
  break
  }
 }
 }
 } 
 })
} else if let error = authError {
 label.text = error.localizedDescription
 // Deal with error
}

Fingerprint and password authentication code


if #available(iOS 9.0, *) {
 let context = LAContext()
 context.localizedFallbackTitle = "Fall back button"
 if #available(iOS 10.0, *) {
 context.localizedCancelTitle = "Cancel button"
 }
 var authError: NSError?
 if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &authError) {
 context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "Localized reason for authentication", reply: { (success, evaluateError) in
 // NOT in main thread
 DispatchQueue.main.async {
 if success {
  self.label.text = "Success"
  // Do something success
 } else if let error = evaluateError {
  self.label.text = error.localizedDescription
  // When fall back button clicked, user is required to enter PIN. Error code will not be "userFallback"
  // Deal with error
 }
 }
 })
 } else if let error = authError {
 label.text = error.localizedDescription 
 // Deal with error
 }
} else {
 let alert = UIAlertController(title: nil, message: "Authentication is available on iOS 9.0 or later", preferredStyle: .alert)
 alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
 present(alert, animated: true, completion: nil)
}

Related articles: