Aleem
Aleem

Reputation: 3291

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.

Upvotes: 8

Views: 10465

Answers (6)

Bilal Meniz
Bilal Meniz

Reputation: 11

If biometric controls are defined on the device, you can find out which control it is. However, if the user has not defined face ID or touch ID on the device, the code will fail. If the device has biometric control and is not used, you can find it through these error codes. Detailed information for error codes; https://developer.apple.com/documentation/localauthentication/laerror/laerrorbiometrynotenrolled

public static var hasBiometricControl: Bool {
        
        let authContext = LAContext()
        var error: NSError?
        guard authContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) else {
            
            switch Int32(error!.code) {
            case kLAErrorBiometryNotAvailable:
                return false
            case kLAErrorBiometryNotEnrolled:
                return true
            default:
                return false
            }
        }

        if #available(iOS 11.0, *) {
            switch authContext.biometryType {
            case .none:
                return false
            case .touchID:
                return true
            case .faceID:
                return true
            @unknown default:
                return false
            }
        }
        return  authContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) ? true: false
    }

Upvotes: 0

MoLowKey
MoLowKey

Reputation: 1142

+(BOOL)supportFaceID
{
   LAContext *myContext = [[LAContext alloc] init];
   NSError *authError = nil;
   // call this method only to get the biometryType and we don't care about the result!
   [myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError];
    NSLog(@"%@",authError.localizedDescription);
    if (@available(iOS 11.0, *)) {
    return myContext.biometryType == LABiometryTypeFaceID;
   } else {
    // Device is running on older iOS version and OFC doesn't have FaceID
    return NO;
  }
}

Upvotes: 0

rockdaswift
rockdaswift

Reputation: 9983

Swift 4 compatible version

var isFaceIDSupported: Bool {
    if #available(iOS 11.0, *) {
        let localAuthenticationContext = LAContext()
        if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) {
            return localAuthenticationContext.biometryType == .faceID
        }
    }
    return false
}

Upvotes: 2

Aleem
Aleem

Reputation: 3291

Thanks Ashley Mills, I created a function to detect FaceID in Device.

- (BOOL)canAuthenticateByFaceID {
    LAContext *context = [[LAContext alloc] init];
    if context.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error) {
       if (context.biometryType == LABiometryTypeFaceID && @available(iOS 11.0, *)) {
        return YES;
    } else {
        return NO;
    }
  }
}

Hope this will help other. Happy coding!!

Finally I wrote my own Library for detecting FaceID here you find

Upvotes: 5

Stu P.
Stu P.

Reputation: 1413

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.

Upvotes: 11

Hai Hw
Hai Hw

Reputation: 1447

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;
}

Upvotes: 12

Related Questions