美文网首页
TouchID的使用

TouchID的使用

作者: ZYiDa | 来源:发表于2017-06-01 16:41 被阅读19次

    第一步,先导入#import <LocalAuthentication/LocalAuthentication.h>头文件
    第二步,设置TouchID对象

    LAContext *touchID = [LAContext new];
    NSError *error = nil;
    NSString *touchIDSuccess = @"XXOO正在申请通过指纹验证!";
    

    其中,[touchID canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]用来验证TouchID是否可用,
    [touchID evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:touchIDSuccess reply:^(BOOL success, NSError * _Nullable error){ }];用来进行指纹验证。
    具体的代码如下:

    - (IBAction)popTouchID:(id)sender
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            LAContext *touchID = [LAContext new];
            NSError *error = nil;
            NSString *touchIDSuccess = @"XXOO正在申请通过指纹验证!";
            if ([touchID canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                                     error:&error])
            {//支持touchID
                [touchID evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:touchIDSuccess reply:^(BOOL success, NSError * _Nullable error)
                {
    
                    NSLog(@"ErrorCode:%ld",error.code);
    
                    switch (error.code)
                    {
                        case LAErrorAuthenticationFailed:
                        {
                            [self alertWithMessage:@"指纹验证失败"];
                        }
                            break;
                        case LAErrorUserCancel:
                        {
                            [self alertWithMessage:@"用户取消验证"];
                        }
                            break;
                        case LAErrorUserFallback:
                        {
                            [self alertWithMessage:@"用户进行了后退操作"];
                        }
                            break;
                        case LAErrorSystemCancel:
                        {
                            [self alertWithMessage:@"系统取消操作"];
                        }
                            break;
                        case LAErrorPasscodeNotSet:
                        {
                            [self alertWithMessage:@"用户尚未设置密码"];
                        }
                            break;
                        case LAErrorTouchIDNotEnrolled:
                        {
                            [self alertWithMessage:@"用户尚未设置 TouchID"];
                        }
                            break;
                        case LAErrorTouchIDLockout:
                        {
                            [self alertWithMessage:@"TouchID已停止运行"];
                        }
                            break;
                        case LAErrorAppCancel:
                        {
                            [self alertWithMessage:@"APP取消操作"];
                        }
                            break;
                        case LAErrorInvalidContext:
                        {
                            [self alertWithMessage:@"TouchID不可用"];
                        }
                            break;
                        case 0:
                        {
                            [self alertWithMessage:@"TouchID验证成功"];
                        }
                            break;
                        default:
                            break;
                    }
                }];
            }
            else
            {//不支持touchID,或者错误次数太多,造成touchID不可用
                [self alertWithMessage:@"TouchID不可用"];
            }
        });
    }
    
    - (void)alertWithMessage:(NSString *)message
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Tips"
                                                                                     message:message
                                                                              preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"Cancel"
                                                             style:UIAlertActionStyleCancel
                                                           handler:^(UIAlertAction * _Nonnull action) {
    
                                                           }];
            [alertController addAction:action];
            [self presentViewController:alertController animated:YES completion:nil];
        });
    }
    
    

    不足之处,还请多多指教。

    相关文章

      网友评论

          本文标题:TouchID的使用

          本文链接:https://www.haomeiwen.com/subject/cseefxtx.html