由于最近在項目中很多處都需要使用指紋驗證 ?最好的辦法就是封裝起來 ?以下是我用block封裝的類方法
Appledelegate.h
@interface AppDelegate : UIResponder
+(void)sureUserWith:(void (^)())successBlock failBlock:(void (^)(NSString *errorMessage,BOOL isAlt))failBlock;
@end
Appledelegate.m
+(void)sureUserWith:(void (^)())successBlock failBlock:(void (^)(NSString *errorMessage,BOOL isAlt))failBlock;
{
[SVProgressHUD show];
//初始化上下文對象
LAContext* context = [[LAContext alloc] init];
context.localizedFallbackTitle = @"";
//錯誤對象
NSError* error = nil;
NSString* result = @"通過Home鍵驗證已有手機指紋";
//首先使用canEvaluatePolicy 判斷設(shè)備支持狀態(tài)
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
//支持指紋驗證
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {
if (success) {
//驗證成功,主線程處理UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
successBlock();
}];
}
else
{
[SVProgressHUD dismiss];
NSLog(@"%@",error.localizedDescription);
switch (error.code) {
case LAErrorSystemCancel:
{
failBlock(@"切換到其他APP,系統(tǒng)取消驗證Touch ID",NO);
//切換到其他APP,系統(tǒng)取消驗證Touch ID
break;
}
case LAErrorUserCancel:
{
failBlock(@"用戶取消驗證Touch ID",NO);
//用戶取消驗證Touch ID
break;
}
case LAErrorUserFallback:
{
NSLog(@"User selected to enter custom password");
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//用戶選擇其他驗證方式,切換主線程處理
}];
break;
}
default:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//其他情況,切換主線程處理
//多次錯誤
failBlock(@"您已多次錯誤,請重新登錄",YES);
}];
break;
}
}
}
}];
}
else
{
//不支持指紋識別,LOG出錯誤詳情
switch (error.code) {
case LAErrorTouchIDNotEnrolled:
{
failBlock(@"設(shè)備不支持指紋解鎖",NO);
break;
}
case LAErrorPasscodeNotSet:
{
failBlock(@"設(shè)備未設(shè)置指紋解鎖",NO);
break;
}
case -8:
{
failBlock(@"系統(tǒng)指紋已被鎖定,請選擇其他登錄方式",YES);
break;
}
case -1:
{
//連續(xù)錯誤三次
[SVProgressHUD dismiss];
failBlock(@"抱歉,您未能通過Touch ID指紋驗證!",YES);
break;
}
default:
{
failBlock(@"設(shè)備未設(shè)置touchId",NO);
break;
}
}
}
}
有不足的地方還希望大家能指正以下~