由于最近要使用指紋解鎖app一铅,就暫且了解了一下陕贮,注釋非常詳細。需要注意的是在successBlock里面代碼是后臺線程執(zhí)行的潘飘。
如果需要放在主線程肮之,可添加到主線程上執(zhí)行:
1掉缺、點擊屏幕,彈出Touch ID框
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// ios8.0以上 iphone5s之后才有touchID功能
[self authenticateUser];
}
2戈擒、實現(xiàn)方法
- (void)authenticateUser
{
//初始化上下文對象
LAContext* context = [[LAContext alloc] init];
//localizedFallbackTitle設(shè)置為@""代表指紋輸錯不會出現(xiàn)右側(cè)“輸入密碼”字樣
context.localizedFallbackTitle = @"";
//錯誤對象
NSError* error = nil;
NSString* result = @"xxx利用你的Touch ID解鎖";
//首先使用canEvaluatePolicy 判斷設(shè)備是否支持Touch ID的狀態(tài)
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) { // 主線程
//支持指紋驗證
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError \*error) {
#warning 后臺線程
if (success) {
//驗證成功眶明,后臺線程
NSLog(@"驗證成功");
}
else
{
switch (error.code) {
case LAErrorSystemCancel:
{
//切換到其他APP搜囱,系統(tǒng)取消驗證Touch ID 其他app切入
NSLog(@"切換到其他APP柑土,系統(tǒng)取消驗證Touch ID 其他app切入Authentication was cancelled by the system");
break;
}
case LAErrorAppCancel:
{
NSLog(@"用戶不能控制的掛起 比如打電話");
break;
}
case LAErrorUserCancel:
{
//用戶取消驗證Touch ID
NSLog(@"用戶取消驗證Touch IDAuthentication was cancelled by the user");
break;
}
case LAErrorUserFallback:
{
NSLog(@"用戶選擇輸入密碼,切換主線程處理User selected to enter custom password");
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//用戶選擇輸入密碼幌缝,切換主線程處理
}];
break;
}
case LAErrorTouchIDLockout:
{
NSLog(@"多次TouchID失敗 Touch ID被鎖");// 輸入多次不正確 執(zhí)行代碼
break;
}
case LAErrorInvalidContext:
{
NSLog(@"LAContext對象被釋放掉了诫欠,造成的授權(quán)失敗");
break;
}
default:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//其他情況,切換主線程處理
}];
break;
}
}
NSLog(@"%@",error.localizedDescription);
}
}];
}
else{ //不支持指紋識別轿偎,LOG出錯誤詳情被廓。比如設(shè)備不支持或者指紋沒開啟(指紋沒開啟也包括輸入錯誤多次被鎖定Touch ID)
#warning 主線程
switch (error.code) {
case LAErrorTouchIDNotEnrolled:
{
// 設(shè)備Touch ID不可用 用戶未錄入
NSLog(@"設(shè)備Touch ID不可用 用戶未錄入TouchID is not enrolled");
break;
}
case LAErrorPasscodeNotSet:
{
// 系統(tǒng)未設(shè)置密碼
NSLog(@"系統(tǒng)未設(shè)置密碼A passcode has not been set");
break;
}
case LAErrorTouchIDNotAvailable:
{
// 設(shè)備Touch ID不可用,例如未打開
NSLog(@"設(shè)備Touch ID不可用嫁乘,例如未打開A passcode has not been set");
break;
}
default:
{
// Touch ID被鎖定之后,點擊屏幕跳到這里
NSLog(@"TouchID not available");
break;
}
}
NSLog(@"%@",error.localizedDescription);
}
}