指纹验证

/*
 操作系统最低为iOS8.0

 做iOS8.0下版本适配时,务必进行API验证,避免调用相关API引起崩溃。

 引入依赖框架:#import <LocalAuthentication/LocalAuthentication.h>

 */
#import "ViewController.h"
#import <Masonry.h>
#import <LocalAuthentication/LocalAuthentication.h>



@interface ViewController ()

@property (nonatomic ,strong) UIButton *touchBtn;

@end

@implementation ViewController

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

    [self.view addSubview:self.touchBtn];

    __weak typeof (self) weakSelf = self;

    [self.touchBtn mas_makeConstraints:^(MASConstraintMaker *make) {

        make.center.equalTo(weakSelf.view);
        make.size.mas_equalTo(CGSizeMake(80, 80));

    }];
}

-(void)setLocalAuthentication
{
    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(@"-----------验证成功");
                [self setAlertViewWithTitle:@"验证成功"];
            }
            else
            {
                NSLog(@"%@",error.localizedDescription);

                switch (error.code)
                {
                    case LAErrorSystemCancel:
                    {
                        NSLog(@"切换到其他APP,系统取消验证Touch ID");
                        [self setAlertViewWithTitle:@"系统取消"];
                        break;
                    }
                    case LAErrorUserCancel:
                    {
                        NSLog(@"用户取消验证Touch ID");
                        [self setAlertViewWithTitle:@"用户取消验证"];
                        break;
                    }
                    case LAErrorUserFallback:
                    {
                        NSLog(@"用户选择其他验证方式,切换主线程处理");
                        [self setAlertViewWithTitle:@"用户选择其他验证方式"];
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                        }];
                        break;
                    }
                    default:
                    {
                        NSLog(@"其他情况,切换主线程处理");
                        [self setAlertViewWithTitle:@"其他情况"];
                        [[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(@"不支持指纹识别");
        [self setAlertViewWithTitle:@"不支持指纹识别"];
    }

}
-(void)setAlertViewWithTitle:(NSString *)title
{
    if ([[UIDevice currentDevice].systemVersion floatValue] >=9.0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:title preferredStyle:  UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }]];
        [self presentViewController:alert animated:true completion:nil];

    }else{
        UIAlertView *alt = [[UIAlertView alloc] initWithTitle:@"提示" message:title delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alt show];
    }
}
-(UIButton *)touchBtn
{
    if (!_touchBtn) {
        _touchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_touchBtn setTitle:@"指纹验证" forState:UIControlStateNormal];
        [_touchBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_touchBtn setBackgroundColor:[UIColor yellowColor]];
        [_touchBtn addTarget:self action:@selector(setLocalAuthentication) forControlEvents:UIControlEventTouchUpInside];
    }
    return _touchBtn;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值