4

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

谢谢!

4

5 回答 5

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

更新

我忘了,检查一下:我们如何以编程方式检测设备运行在哪个 iOS 版本上?界定SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO

于 2014-11-25T10:19:01.883 回答
5

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

参数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];

    }
}

学习此功能的好教程在这里

于 2014-11-25T10:23:24.997 回答
4

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

此设备不提供生物识别功能。

如果您使用的是 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 ();
            }
        }
于 2016-04-19T21:07:49.240 回答
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;
}
于 2015-03-12T18:57:51.230 回答
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.");
}
于 2016-09-04T19:27:45.110 回答