5

Sorry, unavailability of iPhone-X.

After the launch of iPhone-X, everyone wants their application should be compatible with iOS11 and with touchID but the problem is it's too expensive for a developer to test touch ID.

I don't have iPhone to check my code but can I check the same in iOS simulator?

let context = LAContext()
if ( context.biometryType == .typeFaceID ) {
      // Face ID
}
if ( context.biometryType == .typeTouchID) {
     // Touch ID
} else {
    // Stone Age
}
Krunal
  • 68,602
  • 40
  • 230
  • 241
Samrez Ikram
  • 413
  • 4
  • 12

1 Answers1

11

You can test it without device also. Use simulator's Face ID to validate your code and it will behave similarly in iPhone-X also.

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

Krunal
  • 68,602
  • 40
  • 230
  • 241
  • 4
    Good answer. A key bit that's easily lost in all this code — the `biometryType` property has nothing to do with *implementing* Face ID support. Checking enrollment and authenticating are the same `canEvaluatePolicy` and `evaluatePolicy` calls you use for Touch ID. Use `biometryType` not as part of your authentication logic, but to tell users what kind of authentication is in use. (Think of all the bank apps that give you a "use Touch ID" checkbox after your first login — they need to know to say "Face ID" instead.) – rickster Nov 08 '17 at 21:15
  • @rickster you are welcomed to edit and improve this answer. Please share your inputs by editing this answer. – Krunal Nov 09 '17 at 03:45