通過LAContext的使用席舍,調(diào)用系統(tǒng)指紋解鎖功能來實現(xiàn)APP的解鎖。
代碼封裝
接口文件.h
#import <Foundation/Foundation.h>
@interface AuthenManager : NSObject
- (void)authen:(void (^)(void))start complete:(void (^)(BOOL success, NSInteger errorCode))complete;
@end
實現(xiàn)文件.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
{
// 開始回調(diào)
if (start) {
start();
}
// 判斷設(shè)備是否支持指紋識別
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(@"系統(tǒng)取消授權(quán)哮笆,如其他APP切入");
break;
}
case LAErrorUserCancel:
{
NSLog(@"用戶取消驗證Touch ID");
break;
}
case LAErrorAuthenticationFailed:
{
NSLog(@"授權(quán)失敗");
break;
}
case LAErrorPasscodeNotSet:
{
NSLog(@"系統(tǒng)未設(shè)置密碼");
break;
}
case LAErrorTouchIDNotAvailable:
{
NSLog(@"設(shè)備Touch ID不可用来颤,例如未打開");
break;
}
case LAErrorTouchIDNotEnrolled:
{
NSLog(@"設(shè)備Touch ID不可用汰扭,用戶未錄入");
break;
}
case LAErrorUserFallback:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"用戶選擇輸入密碼,切換主線程處理");
}];
break;
}
default:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"其他情況脚曾,切換主線程處理");
}];
break;
}
}
}
// 成功回調(diào)
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);
// 失敗回調(diào)
if (complete) {
complete(NO, error.code);
}
}
}
- (LAContext *)context
{
if (_context == nil) {
// 創(chuàng)建LAContext
_context = [LAContext new];
// 這個設(shè)置的使用密碼的字體,當text=@""時启具,按鈕將被隱藏本讥,也設(shè)置指紋輸入失敗之后的彈出框的選項
_context.localizedFallbackTitle = @"密碼解鎖";
// 這個設(shè)置的取消按鈕的字體
_context.localizedCancelTitle = @"取消";
}
return _context;
}
@end
封裝類使用
#import "ViewController.h"
// 導(dǎo)入封裝類頭文件
#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 = @"已經(jīng)進行指紋解鎖拷沸!可以進行一下步操作了~";
} else {
weakSelf.authenLabel.text = @"【指紋】解鎖失敗薯演!";
weakSelf.message = @"【指紋】解鎖失斪采帧!";
}
}];
}
- (void)alertClick
{
[[[UIAlertView alloc] initWithTitle:nil message:self.message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"知道了", nil] show];
}
@end