如果设备支持Touch ID

时间:2014-11-25 10:15:11

标签: ios objective-c ios8 touch-id

想知道我如何确定用户是否支持Touch ID API的设备?希望将此值作为布尔值。

谢谢!

5 个答案:

答案 0 :(得分:5)

试试这个:

- (BOOL)canAuthenticateByTouchId {
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
        return [[[LAContext alloc] init] canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil];
    }
    return NO;
}

或者像@rckoenes建议:

- (BOOL)canAuthenticateByTouchId {
    if ([LAContext class]) {
        return [[[LAContext alloc] init] canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil];
    }
    return NO;
}

<强>更新

我忘记了,请检查:How can we programmatically detect which iOS version is device running on?来定义SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO

答案 1 :(得分:5)

您应该考虑触摸ID身份验证所需的LAContext框架。

参数LAErrorTouchIDNotAvailable将显示设计支持此功能。

代码段:

- (IBAction)authenticateButtonTapped:(id)sender {
    LAContext *context = [[LAContext alloc] init];

    NSError *error = nil;

    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
        // Authenticate User

    } else {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Your device cannot authenticate using TouchID."
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
        [alert show];

    }
}

了解此功能的好教程是here

答案 2 :(得分:4)

您可以使用CanEvaluatePolicy检查错误。如果错误代码为-6,则表示该设备上没有物理Touch Id。你可以从错误描述中看出来,它说

  

此设备无法使用Biometry。

以下是您使用C#Xamarin时的代码:

var context = new LAContext();
        NSError AuthError;
        if (!context.CanEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, out AuthError))
        {
            if ( AuthError != null && AuthError.Code == -6 )
            {
                var alert = new UIAlertView ("Error", "TouchID not available", null, "BOOO!", null);
                alert.Show ();
            }
        }

答案 3 :(得分:0)

这个功能对此有帮助 -

-(BOOL)doesThisDeviceSupportTouchIdForLocalAuthentication{

    //Checking for 64 bit (armv7s) architecture before including the LAContext as it would give error otherwise.
    #if TARGET_CPU_ARM64
    LAContext *context = [[LAContext alloc] init];

    NSError *error = nil;

    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]){
        return YES;
    }
    return NO;
    #endif

    return NO;
}

答案 4 :(得分:0)

目标c

@import LocalAuthentication;
// Get the local authentication context:
LAContext *context = [[LAContext alloc] init];
// Test if fingerprint authentication is available on the device and a fingerprint has been enrolled.
if ([context canEvaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil])
{
    NSLog(@"Fingerprint authentication available.");
}