iOS - Face ID biometric integration
Asked Answered
E

2

10

I've integrated/implemented Face ID (Local Authentication) authentication for my app and everything works fine, except Face ID prompt Alert window interface.

It shows, a rounded square with a light gray background and the title "Face ID".

What should need to set for blank area exact above title? Is that space for face id icon? if yes then how can I set it? I've tried everything in LAContext and LAPolicy.

Look at this snapshot:

enter image description here

Here is my code:

    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, *) {
            switch laContext.biometryType {
            case .faceID: localizedReason = "Unlock using Face ID"; print("FaceId support")
            case .touchID: localizedReason = "Unlock using Touch ID"; print("TouchId support")
            case .none: 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")
                    }
                }

            })
        })
    }
Emancipated answered 8/11, 2017 at 15:8 Comment(0)
M
22

That is only happen in simulator, in actual device the canvas is occupied by face icon animation.

The localizedReason is for only for Touch ID, since they both sharing the same API.

Update 1: Added screen recordings:

They all ran the same code:

func beginFaceID() {

    guard #available(iOS 8.0, *) else {
        return print("Not supported")
    }

    let context = LAContext()
    var error: NSError?

    guard context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) else {
        return print(error)
    }

    let reason = "Face ID authentication"
    context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { isAuthorized, error in
        guard isAuthorized == true else {
            return print(error)
        }

        print("success")
    }

}

Here is Working code for both TouchID & FaceID with all Error Codes (Swift 4)

https://mcmap.net/q/1055884/-face-id-evaluation-process-not-working-properly

Malena answered 9/11, 2017 at 14:50 Comment(1)
Can you please share visual effect of actual device, if you've it. I just want to make sure, my app is working fine, along with proper user interface before I deploy/upload it on app store.Emancipated
M
3

In any project that uses biometrics, include the NSFaceIDUsageDescription key in your app’s Info.plist file. Without this key, the system won’t allow your app to use Face ID.

let authContext = LAContext()
        authContext.localizedFallbackTitle = "Use Passcode"
        authContext.localizedCancelTitle = "Cancel"

var authError: NSError?

if authContext.canEvaluatePolicy(.deviceOwnerAuthentication, error: &authError) {
   evaluatePolicy(policy, context: authContext)
} else {
     guard let error = authError else { return }
     print("Error: \(error.code)")
     checkError(error)
}

private func evaluatePolicy(_ policy: LAPolicy, context: LAContext) {
        context.evaluatePolicy(policy, localizedReason: reason) { (success, error) in
            if success {
                print("Success")

            } else {
                guard let error = error else { return }
                self.checkError(error as NSError)
            }
        }
}

private func checkError(_ error: NSError) {
    guard let error = error as? LAError else { return }

    switch error.code {
    case .authenticationFailed:
        print("authenticationFailed")
        requestAuth(policy: .deviceOwnerAuthentication)
    case .userFallback:
        print("userFallback")
        requestAuth(policy: .deviceOwnerAuthentication)
    case .userCancel:
        print("userCancel")
    case .systemCancel:
        print("systemCancel")
    case .passcodeNotSet:
        print("passcodeNotSet")
    case .appCancel:
        print("appCancel")
    case .invalidContext:
        print("invalidContext")
    case .notInteractive:
        print("notInteractive")
    default:
        checkBioMetricError(error)
    }
}

private func checkBioMetricError(_ error: LAError) {
    if #available(iOS 11.0, *) {

        switch error.code {
        case .biometryNotAvailable,
             .biometryNotEnrolled,
             .biometryLockout:
            requestAuth(policy: .deviceOwnerAuthentication)

        default: break
        }
    } else {
        switch error.code {

        case .touchIDNotAvailable,
             .touchIDNotEnrolled,
             .touchIDLockout:
            requestAuth(policy: .deviceOwnerAuthentication)

        default: break
        }
    }
}
Mealymouthed answered 30/9, 2019 at 6:42 Comment(2)
I dont think this is the case. the given code is already working for faceid just icon is missing.Anselmo
NSFaceIDUsageDescription. this saved my time.Craggie

© 2022 - 2024 — McMap. All rights reserved.