UIiOS一点通

指纹解锁功能LAContext的使用

2018-07-16  本文已影响139人  番薯大佬

通过LAContext的使用,调用系统指纹解锁功能来实现APP的解锁。

代码封装
接口文件.h

#import <Foundation/Foundation.h>

@interface AuthenManager : NSObject

- (void)authen:(void (^)(void))start complete:(void (^)(BOOL success, NSInteger errorCode))complete;

@end

实现文件.m

#import "AuthenManager.h"

// 指纹解锁必须的头文件
#import <LocalAuthentication/LocalAuthentication.h>

@interface AuthenManager ()

@property (nonatomic, strong) LAContext *context;

@end

@implementation AuthenManager

- (void)authen:(void (^)(void))start complete:(void (^)(BOOL success, NSInteger errorCode))complete
{
    // 开始回调
    if (start) {
        start();
    }
    
    // 判断设备是否支持指纹识别
    NSError *error = nil;
    BOOL isValidTouch = [self.context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];
    if (isValidTouch)
    {
        NSLog(@"支持指纹识别");
        [self.context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"请按home键指纹解锁" reply:^(BOOL success, NSError * _Nullable error) {
            if (success)
            {
                NSLog(@"验证成功 刷新主界面");                
            }
            else
            {
                NSLog(@"%@", error.localizedDescription);
                switch (error.code)
                {
                    case LAErrorSystemCancel:
                    {
                        NSLog(@"系统取消授权,如其他APP切入");
                        break;
                    }
                    case LAErrorUserCancel:
                    {
                        NSLog(@"用户取消验证Touch ID");
                        break;
                    }
                    case LAErrorAuthenticationFailed:
                    {
                        NSLog(@"授权失败");
                        break;
                    }
                    case LAErrorPasscodeNotSet:
                    {
                        NSLog(@"系统未设置密码");
                        break;
                    }
                    case LAErrorTouchIDNotAvailable:
                    {
                        NSLog(@"设备Touch ID不可用,例如未打开");
                        break;
                    }
                    case LAErrorTouchIDNotEnrolled:
                    {
                        NSLog(@"设备Touch ID不可用,用户未录入");
                        break;
                    }
                    case LAErrorUserFallback:
                    {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            NSLog(@"用户选择输入密码,切换主线程处理");
                        }];
                        break;
                    }
                    default:
                    {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            NSLog(@"其他情况,切换主线程处理");
                        }];
                        break;
                    }
                }
            }
            
            // 成功回调
            if (complete) {
                complete(success, error.code);
            }
        }];
    }
    else
    {
        NSLog(@"不支持指纹识别");
        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);
        
        // 失败回调
        if (complete) {
            complete(NO, error.code);
        }
    }
}

- (LAContext *)context
{
    if (_context == nil) {
        // 创建LAContext
        _context = [LAContext new];
        // 这个设置的使用密码的字体,当text=@""时,按钮将被隐藏,也设置指纹输入失败之后的弹出框的选项
        _context.localizedFallbackTitle = @"密码解锁";
        // 这个设置的取消按钮的字体
        _context.localizedCancelTitle = @"取消";
    }
    return _context;
}

@end

封装类使用

#import "ViewController.h"
// 导入封装类头文件
#import "AuthenManager.h"

@interface ViewController ()

@property (nonatomic, strong) AuthenManager *authenManager;
@property (nonatomic, strong) UILabel *authenLabel;
@property (nonatomic, strong) NSString *message;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.navigationItem.title = @"指纹解锁";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"authen" style:UIBarButtonItemStyleDone target:self action:@selector(authenClick)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"alert" style:UIBarButtonItemStyleDone target:self action:@selector(alertClick)];
    
    self.authenLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 10.0, (self.view.frame.size.width - 10.0 * 2), 40.0)];
    [self.view addSubview:self.authenLabel];
    self.authenLabel.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.2];
    self.authenLabel.textAlignment = NSTextAlignmentCenter;
    self.authenLabel.text = @"等待解锁~";
    //
    self.message = @"请先进行指纹解锁!";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)authenClick
{
    if (self.authenManager == nil) {
        self.authenManager = [[AuthenManager alloc] init];
    }
    
    ViewController __weak *weakSelf = self;
    [self.authenManager authen:^{
        weakSelf.navigationItem.leftBarButtonItem.enabled = NO;
        weakSelf.navigationItem.rightBarButtonItem.enabled = NO;
        
        weakSelf.authenLabel.text = @"正在进行【指纹】解锁……";
    } complete:^(BOOL success, NSInteger errorCode) {
        weakSelf.navigationItem.leftBarButtonItem.enabled = YES;
        weakSelf.navigationItem.rightBarButtonItem.enabled = YES;
        
        if (success) {
            weakSelf.authenLabel.text = @"【指纹】解锁成功!";
            weakSelf.message = @"已经进行指纹解锁!可以进行一下步操作了~";
        } else {
            weakSelf.authenLabel.text = @"【指纹】解锁失败!";
            weakSelf.message = @"【指纹】解锁失败!";
        }
    }];
}

- (void)alertClick
{
    [[[UIAlertView alloc] initWithTitle:nil message:self.message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"知道了", nil] show];
}

@end
上一篇下一篇

猜你喜欢

热点阅读