iOS Touch ID authentication

Keywords: iOS github

iOS Touch ID authentication

iOS 8 and devices that record fingerprints in the future can use touch ID to authenticate, and fingerprints that match the input fingerprints can authenticate successfully.

step

  1. Import Local Authentication Framework: import Local Authentication
  2. Initialize the LAContext object: let context = LAContext()
  3. Call the canEvaluatePolicy (policy: LAPolicy, error: NSErrorPointer) - > Bool method of the LAContext object
  4. If the previous step returns false, it means that authentication cannot be performed and the corresponding failed operation is performed; if it returns true, it calls the evaluatePolicy (policy: LAPolicy, localized Reason: String, reply: @escaping (Bool, Error?) -> Void) method of the LAContext object to determine whether the authentication successfully performs the corresponding operation in reply (if authentication fails, the error code can be obtained). Code, see which type of error belongs to LAError.Code to perform the corresponding failed operation)

Both the canEvaluatePolicy and evaluatePolicy methods that call the LAContext object pass in values of the LAPolicy enumeration type. There are currently two values: deviceOwnerAuthentication WithBiometrics and deviceOwnerAuthentication. The former kind of device Owner Authentication WithBiometrics is fingerprint authentication. The latter kind of device Owner Authentication is iOS 9.0 and can only be used later. First, fingerprint authentication is carried out. If fingerprint authentication fails, it can be authenticated by entering a password.

Calling the evaluatePolicy method of the LAContext object pops up the fingerprint authentication dialog box. The dialog box displays the reason for the need for authentication (String), which is the value of the localized Reason parameter. The dialog box has a Cancel button, iOS 10.0 and later can set the localized CancelTitle value of the LAContext object to change the word displayed by the Cancel button. If fingerprint authentication fails, the dialog box also displays the fallback button, which can change the word displayed by the fallback button by setting the value of the localized Fallback Title of the LAContext object.

It should be noted that the reply callback of the evaluatePolicy method is not in the main thread. If you need to update the UI, call the main thread to update it.

Code example

The code has been uploaded to GitHub: https://github.com/Silence-GitHub/TouchIDDemo
Place a label in the controller to display the result of authentication return.

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)
}

For reprinting, please indicate the source: http://www.cnblogs.com/silence-cnblogs/p/6397148.html

Posted by rinjani on Wed, 27 Mar 2019 23:45:30 -0700