如何检测设备中是否配置了 Touch ID

How To detect Touch ID is Configured or not in device

我正在使用下面的代码来检测设备中 Touch ID 的可用性,它工作正常

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

    return NO;
}
- (IBAction)touchIDAvailability
{
    if([self canAuthenticateByTouchId])
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Congrats"
                                                        message:@"Your device have TouchID"
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
        [alert show];
    }
    else
    {

        str = @"TouchID is not available in your device";
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops"
                                                        message:str
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
        [alert show];

    }
}

检测到 Touch ID 可用性后,我想确定 Touch ID 是否已配置

NSString *str;

if (LAErrorTouchIDNotEnrolled)
{
    str = @"Please configure your TouchID in Settings > Touch ID & Passcode";
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops"
                                                            message:str
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
[alert show];

我配置了 Touch ID 但它仍然进入了 LAErrorTouchIDNotEnrolled 这个循环。有知道的可以告诉我吗

您可以使用下面的方法检查用户是否已配置

[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                          localizedReason:myLocalizedReasonString
                                    reply:^(BOOL succes, NSError *error) {
}

这是 Touch ID 的完整代码

LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
myContext.localizedFallbackTitle=@"Login with Password";//optional for hiding enter password button
NSString *myLocalizedReasonString = @"For touch ID Users Ignore this pop-up and proceed to login with you finger now";

        if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {

            [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                      localizedReason:myLocalizedReasonString
                                reply:^(BOOL succes, NSError *error) {
                                    if (succes) {
                                       [self showMessage:@"Authentication is successful" withTitle:@"Success"];
                                 NSLog(@"User authenticated");                                        }
                                    } else {

                                        switch (error.code) {
                                            case LAErrorAuthenticationFailed:
                                                [self showMessage:@"Authentication is failed" withTitle:@"Error"];
                                                NSLog(@"Authentication Failed");
                                                break;

                                            case LAErrorUserCancel:
                                               [self showMessage:@"You clicked on Cancel" withTitle:@"Error"];
                                                NSLog(@"User pressed Cancel button");
                                                break;

                                            case LAErrorUserFallback:
                                                [self showMessage:@"You clicked on \"Enter Password\"" withTitle:@"Error"];
                                                NSLog(@"User pressed \"Enter Password\"");
                                                break;

                                            default:
                                                [self showMessage:@"Touch ID is not configured" withTitle:@"Error"];
                                                NSLog(@"Touch ID is not configured");
                                                break;
                                        }

                                        NSLog(@"Authentication Fails");
                                    }
                                }];
        } else {
    NSLog(@"Can not evaluate Touch ID");

    NSString *str = @"TouchID is not available in your device";
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops"
                                                    message:str
                                                   delegate:nil
                                          cancelButtonTitle:@"Ok"
                                          otherButtonTitles:nil];
    [alert show];
        }
    }


-(void) showMessage:(NSString*)message withTitle:(NSString *)title
{
    [[[UIAlertView alloc] initWithTitle:@"AppName" message:message delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil] show];
}

希望对你有帮助..!