iOS 11 Simulator not allowing LAContext and FaceID

7.3k views Asked by At

I've running the latest Xcode 9 GM (13 Sep 2017) and have set Hardware > Face ID > Enrolled in simulator as well as Deployment Target 11.0. However I'm getting error code -6 LAErrorTouchIDNotAvailable.

Is there some setting I'm missing?

let myContext = LAContext()
let myLocalizedReasonString = "You are pretty"

var authError: NSError?
if #available(iOS 8.0, macOS 10.12.1, *) {
    if myContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
        myContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: myLocalizedReasonString) { success, evaluateError in
            if success {

                print("// User authenticated successfully, take appropriate action")
            } else {
                 print(" // User did not authenticate successfully, look at error and take appropriate action")
            }
        }
    } else {
         print(" // Could not evaluate policy; look at authError and present an appropriate message to user")
    }
} else {
     print(" // Fallback on earlier versions")
}
5

There are 5 answers

0
russbishop On BEST ANSWER

Face ID does not work in the Xcode 9 GM due to a framework bug. Xcode 9.1 fixes this issue.

You can test your app in the iPhone 8 simulator and ensure it works correctly with Touch ID or run the Xcode 9.1 beta and test Face ID support there.

0
adrian chen On

I think the iphone X simulator's faceID doesn't work at the moment, hopefully they will fix it soon...

https://forums.developer.apple.com/thread/86779

we could do a bug report to see if it speed things along :P https://developer.apple.com/bug-reporting

0
Matt On

XCode 9.1 beta came out today in which the original code should work perfectly in the simulator!

0
T. Hyldgaard On

According to Apples Documentation for LAContext, we need to add the key NSFaceIDUsageDescription with a reason of use String, as that will display the authorisation request for the use of FaceId on the device.

Example add this to info.plist:

NSFaceIDUsageDescription

set it to type String, and add a text that you want to be shown, in the prompt request for access to the Face ID camera.

"Your app" request your permission to use Face ID, for you to login to your account / unlock your notes / what ever reason in the end.

By adding this, you can go to the simulator for iPhone X, and you will be prompted for the Face ID, press accept, and everything should work perfectly.

Remember to enrol biometry support for the simulator by going into Simulator -> Hardware -> Face ID / Touch ID -> Enrolled

Then you just need to pressed the Match / Non-Matching Touch / Face ID, to test out your handling

For more details and check out Apple's documentation: https://developer.apple.com/documentation/localauthentication/lacontext

---- Edit ----

This worked for me in both Xcode 9.0 and 9.1

0
Krunal On

Face ID is working now, with Xcode 9.1. Follow these steps to test it in Simulator.

Add privacy statement in your target's info.plist file.

enter image description here

Import LocalAuthentication framework to your project and 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.

enter image description here

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