如果设备支持Touch ID

Har*_*rry 4 objective-c ios touch-id ios8

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

谢谢!

Mat*_*usz 5

试试这个:

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

或者像@rckoenes建议:

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

UPDATE

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


Ole*_*huk 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];

    }
}
Run Code Online (Sandbox Code Playgroud)

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