开发锦集

指纹识别代码

2017-03-13  本文已影响18人  YvanLiu
1.首先要引入一个头文件
#import <LocalAuthentication/LocalAuthentication.h>

2.具体代码
 // 1.判断是否支持
    if ([UIDevice currentDevice].systemVersion.floatValue < 8.0) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"您的手机不支持指纹识别,请用其他方式登录。" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {}];
        [alertController addAction:cancel];
        [self presentViewController:alertController animated:YES completion:nil];
        return;
    }
    // 2.初始化上下文对象
    LAContext *context = [[LAContext alloc]init];
    // 默认的提示框有两个按钮  一个取消,一个密码登录,这里可以把密码登录
    // 这里把密码登录至为空了
    context.localizedFallbackTitle = @"";
    //错误对象
    NSError *error;
    // 3.首先使用判断设备支持状态
    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
        //支持指纹验证
        // 这里可以修改提示框的默认文字
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedString(@"请验证指纹", nil) reply:
         ^(BOOL success, NSError *authenticationError) {
             if (success) {
                 // 4.成功回调
                 /*
                    具体操作、、、
                  */
             } else {
                 // 5.失败回调
                 /*
                    具体操作、、、
                */                 
             }
         }];
        
    } else {
        // 6.不支持指纹识别
        NSLog(@"\n%@",error);
    }
    
上一篇 下一篇

猜你喜欢

热点阅读