如何检测设备是否支持FaceID?

时间:2017-09-25 10:10:33

标签: objective-c xcode ios11 face-id

它有点早,但我打算专门为FaceID添加功能,所以在此之前我需要验证设备支持FaceID与否? 需要建议和帮助。 提前谢谢。

5 个答案:

答案 0 :(得分:10)

我发现你必须先调用canEvaluatePolicy才能正确获取生物测量类型。如果不这样做,那么原始值总是为0。

在Swift 3中就是这样的,在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
}

你当然可以写一下,看看它是否是生物识别并将类型与bool一起返回,但这对于大多数人来说应该足够了。

答案 1 :(得分:8)

Objective-C版

- (BOOL) isFaceIdSupported{
    if (@available(iOS 11.0, *)) {
        LAContext *context = [[LAContext alloc] init];
        if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]){
            return ( context.biometryType == LABiometryTypeFaceID);
        }
    }
    return NO;
}

答案 2 :(得分:4)

谢谢Ashley Mills,我创建了一个在Device中检测FaceID的函数。

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

希望这会有助于其他人。快乐的编码!!

最后,我编写了自己的库来检测FaceID here you find

答案 3 :(得分:1)

Swift 4兼容版

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

答案 4 :(得分:0)

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