/ ios

Face ID Authentication in iOS11 Swift Language

Apple released most interesting feature Facial recognition with iPhone X. Face ID is a new revolution in recognition. It is a powerful and secure authentication system that’s more convenient, faster and easy than Touch ID.

Face ID is biometric authentication. Biometrics technology is mainly used for identification and access control. Touch ID recognition is also biometric authentication.

Face ID uses LocalAuthentication Framework for authentication and Touch ID uses the same framework for authentication. The LocalAuthentication framework provides facilities for requesting authentication from users. It provides the interface for evaluating authentication policies and access controls and managing credentials with LAContext.

LAContext provides two methods for implementation of Face ID

1. canEvaluatePolicy:

This method check capability of the device for biometric authentication and its returns true if the device is ready for biometric authentication. Otherwise, it returns false and Error code.

 import LocalAuthentication

let localAuthenticationContext = LAContext()
var error: NSError?

if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
   // Device is ready for biometric authentication.
} else {
   // Device is not ready for biometric authentication

2. evaluatePolicy:

This method returns success and error to determining success and error code.

func FaceID() {
    if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
        localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Required for login", reply: { (success, authenticationError) in
            if success {
                // Authentication success
            } else {
                // Authentication fail
    } else {
        // Device is not ready for biometric authentication.

When authentication fails either at canEvaluatePolicy or at evaluatePolicy method, error code is used to get fail reason.
There are many possibilities for fail like Canceled by User, Canceled by Application, User failed to provide valid credentials, too many failed biometry attempt by user etc.