Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect whether device support FaceID or not?

Its bit early to ask but I'm planning to add feature specially for FaceID, so before that I need to validate either device support FaceID or not? Need suggestion and help. Thanks in advance.

like image 611
Aleem Avatar asked Sep 25 '17 10:09

Aleem


2 Answers

Objective-C version

- (BOOL) isFaceIdSupported{
    if (@available(iOS 11.0, *)) {
        LAContext *context = [[LAContext alloc] init];
        if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]){
            return ( context.biometryType == LABiometryTypeFaceID);
        }
    }
    return NO;
}
like image 138
Hai Hw Avatar answered Oct 11 '22 14:10

Hai Hw


I found that you have to call canEvaluatePolicy before you will properly get the biometry type. If you don't you'll always get 0 for the raw value.

So something like this in Swift 3, tested and working in Xcode 9.0 & beta 9.0.1.

class func canAuthenticateByFaceID () -> Bool {
    //if iOS 11 doesn't exist then FaceID doesn't either
    if #available(iOS 11.0, *) {
        let context = LAContext.init()

        var error: NSError?

        if context.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error) {
            //As of 11.2 typeFaceID is now just faceID
            if (context.biometryType == LABiometryType.typeFaceID) {
                return true
            }
        }
    }

    return false
}

You could of course write that just to see if it's either biometric and return the type along with the bool but this should be more than enough for most to work off of.

like image 26
Stu P. Avatar answered Oct 11 '22 12:10

Stu P.