指纹识别 - (Obj-C)

硬件要求: iPhone 5s及以上

软件要求: iOS 8.0 及以上

示例代码:

#import "ViewController.h"
#import 

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //指纹识别 硬件iPhone5S及以上  软件iOS8及以上
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        
        //创建本地认证上下文
        LAContext *context = [[LAContext alloc] init];
        // LAPolicyDeviceOwnerAuthenticationWithBiometrics 指纹认证
        if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {  //可以进行指纹识别
            //进行指针识别  参数1: 验证类型 参数2: 进行识别的原因  参数3: 验证结果
            [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"需要通过指纹进行支付" reply:^(BOOL success, NSError * _Nullable error) {
                if (success) {
                    
                    NSLog(@"识别成功");
                    
                }else {
                    
                    NSLog(@"识别失败: %@", error);
                    
                    switch (error.code) {
                        case -2:
                            NSLog(@"用户取消");
                            break;
                        case -1:  //3次就会停止指纹识别
                            NSLog(@"到达尝试错误次数");
                            break;
                        case -8:  //5次就不再使用指纹识别
                            NSLog(@"取消指纹识别");
                            break;
                        default:
                            break;
                    }
                }
            }];
            NSLog(@"可以进行指纹识别");
        }else {
            NSLog(@"硬件没有达到要求");
        }
    }
    
    
}

@end

你可能感兴趣的:(指纹识别 - (Obj-C))