Can we test Face ID in simulator?

IosSimulatorBiometricsIphone XFace Id

Ios Problem Overview


Can we test biometric authentication using the simulator?

The iPhone X Simulator shows a menu for Face ID enrollment, but after enabling that, what can I do?

How it will recognize a face for authentication?

iPhone X Simulator - Face ID settings

Ios Solutions


Solution 1 - Ios

Simulator does not recognise a face but allows you to simulate a matching and non-matching faces, if you've enabled Enrolled option from Face ID.


Add following code to your view controller and try with Face-ID

import LocalAuthentication

class ViewController: UIViewController {

    
    override func viewDidLoad() {
        super.viewDidLoad()
        localAuthentication()
    }

    
    
    func localAuthentication() -> Void {
        
        let laContext = LAContext()
        var error: NSError?
        let biometricsPolicy = LAPolicy.deviceOwnerAuthenticationWithBiometrics
        
        if (laContext.canEvaluatePolicy(biometricsPolicy, error: &error)) {
            
            if let laError = error {
                print("laError - \(laError)")
                return
            }
            
            var localizedReason = "Unlock device"
            if #available(iOS 11.0, *) {
                if (laContext.biometryType == LABiometryType.faceID) {
                    localizedReason = "Unlock using Face ID"
                    print("FaceId support")
                } else if (laContext.biometryType == LABiometryType.touchID) {
                    localizedReason = "Unlock using Touch ID"
                    print("TouchId support")
                } else {
                    print("No Biometric support")
                }
            } else {
                // Fallback on earlier versions
            }
            
            
            laContext.evaluatePolicy(biometricsPolicy, localizedReason: localizedReason, reply: { (isSuccess, error) in
                
                DispatchQueue.main.async(execute: {
                    
                    if let laError = error {
                        print("laError - \(laError)")
                    } else {
                        if isSuccess {
                            print("sucess")
                        } else {
                            print("failure")
                        }
                    }
                    
                })
            })
        }
        
        
    }
}

FaceID authentication will prompt you for first time to allow FaceID detection for your app.

enter image description here


Now enable Face ID enrolment and run your app to test Face ID simulation Testing.

Here is simulation result for matching and non-matching faces.

Result for matching face:

enter image description here


Result for non-matching face:

enter image description here


Solution 2 - Ios

The simulator just simulates the outcome of a correct and a failed face recognition, just like it does with Touch ID. It does not recognize faces.

Solution 3 - Ios

As you asking but after enabling that, what can I do?

Just like a touch Id enrolment, You can verify things with face-Id on iPhone-X. However simulator have some limitations like Appstore etc. With face-Id enrolment you can do following things -

  • Use Face ID to make purchases.
  • Sign in with Face ID (Sign in to apps).
  • Autofill passwords in Safari.
  • In the iTunes Store, App Store, and iBooks Store.

See more at Apple

Solution 4 - Ios

same as give by @krunal just 2nd if should be outside of 1st.

import LocalAuthentication

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    localAuthentication()
}

func localAuthentication() -> Void {
    
    let laContext = LAContext()
    var error: NSError?
    let biometricsPolicy = LAPolicy.deviceOwnerAuthenticationWithBiometrics
    
    if (laContext.canEvaluatePolicy(biometricsPolicy, error: &error)) {
        
       
        
        var localizedReason = "Unlock device"
        if #available(iOS 11.0, *) {
            if (laContext.biometryType == LABiometryType.faceID) {
                localizedReason = "Unlock using Face ID"
                print("FaceId support")
            } else if (laContext.biometryType == LABiometryType.touchID) {
                localizedReason = "Unlock using Touch ID"
                print("TouchId support")
            } else {
                print("No Biometric support")
            }
        } else {
            // Fallback on earlier versions
        }
        
        
        laContext.evaluatePolicy(biometricsPolicy, localizedReason: localizedReason, reply: { (isSuccess, error) in
            
            DispatchQueue.main.async(execute: {
                
                if let laError = error {
                    print("laError - \(laError)")
                } else {
                    if isSuccess {
                        print("sucess")
                    } else {
                        print("failure")
                    }
                }
            })
        })
    }
//This should be outside of if
   
 if let laError = error {
        print("laError - \(laError)")
        return
     }
 }
}

Solution 5 - Ios

See this article. You can create the Biometrics.m, Biometrics.h, and bridging-header.h files within you UITests folder and update your UI test target to use that bridging header. https://github.com/KaneCheshire/BiometricAutomationDemo

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontechnerdView Question on Stackoverflow
Solution 1 - IosKrunalView Answer on Stackoverflow
Solution 2 - IosTamás SengelView Answer on Stackoverflow
Solution 3 - IosJackView Answer on Stackoverflow
Solution 4 - Iosuser1636255View Answer on Stackoverflow
Solution 5 - IosreutseyView Answer on Stackoverflow