美文网首页
调用系统指纹验证

调用系统指纹验证

作者: 何以_aaa | 来源:发表于2016-12-23 17:03 被阅读31次

    功能需求:登录之后,只要不退出登录,再次进入app时需要验证指纹后进入主页面。

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self showFingerprintVerification];
    }
    
    // 指纹验证
    - (void)showFingerprintVerification {
        
        // 初始化上下文对象
        LAContext *context = [[LAContext alloc] init];
        
        // 错误对象
        NSError *error = nil;
        NSString *result = @"通过Home键验证已有手机指纹";
        
        // 首先使用canEvaluatePolicy 判断设备支持状态
        if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
            
            // 支持指纹验证
            [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result  reply:^(BOOL success, NSError *error) {
                if (success) {
    
                    NSLog(@"登录成功");
                    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                        [[AppDelegate sharedAppDelegate] changeMainRootViewController];
    
                    }];
                    
                } else {
                    NSLog(@"%@",error.localizedDescription);
                    switch (error.code) {
                            
                        case LAErrorSystemCancel: {
                            NSLog(@"切换到其他APP,系统取消验证Touch ID");
                            break;
                        }
                        case LAErrorUserCancel: {
                            NSLog(@"用户取消验证Touch ID");
                            break;
                        }
                        case LAErrorUserFallback: {
                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                NSLog(@"用户选择输入密码,切换主线程处理");
                                
                                // 登录页面
                                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                                [userDefaults setInteger:unLogin forKey:FYLOGINTIMES_Key];
                                [userDefaults synchronize];
                                [[AppDelegate sharedAppDelegate] changeRootViewController];
                            }];
                            break;
                        }
                        case LAErrorTouchIDLockout: { // iOS 9之后才会有的
                            
                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                NSLog(@"多次指纹校验失败, 需要输入密码解锁");
    
                                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"指纹不匹配" message:nil preferredStyle:UIAlertControllerStyleAlert];
                                [self presentViewController:alert animated:NO completion:nil];
                                
                                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 2.0 *NSEC_PER_SEC);
                                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                                    [alert dismissViewControllerAnimated:YES completion:nil];
                                });
                            }];
                            break;
                        }
                        case LAErrorAppCancel: {      // iOS 9之后才会有的
                            NSLog(@"当前app被挂起取消了授权,和LAErrorSystemCancel不太一样");
                            break;
                        }
                        case LAErrorInvalidContext: { // iOS 9之后才会有的
                            NSLog(@"LAContext对象被释放掉了,造成的授权失败");
                            break;
                        }
                        default: {
                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                
                            }];
                            break;
                        }
                    }
                }
            }];
        } else {
            // 不支持指纹识别,LOG出错误详情
            switch (error.code) {
                case LAErrorTouchIDNotEnrolled: {
                    NSLog(@"TouchID is not enrolled");
                    break;
                }
                case LAErrorPasscodeNotSet: {
                    NSLog(@"A passcode has not been set");
                    break;
                }
                default: {
                    NSLog(@"TouchID not available");
                    break;
                }
            }
            NSLog(@"%@",error.localizedDescription);
        }
    }
    

    相关文章

      网友评论

          本文标题:调用系统指纹验证

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