昨天在公司需求中需要加入指纹解锁功能
在指纹接受验证后 回调会在子线程中 因此需要调用主线程执行ui更新
然鹅群里一群人知道却不告诉我 害的我瞎忙活半天。。。。
代码如下
//初始化上下文对象
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) {
//验证成功,主线程处理UI
[mUserdefaults setObject:@"1" forKey:isUseLock];
[mUserdefaults setObject:@"1" forKey:isOpenHand];
[mUserdefaults synchronize];
[SVProgressHUD showSuccessWithStatus:@"开启成功"];
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
[mUserdefaults setObject:@"0" forKey:isOpenHand];
_sw.on = NO;
[SVProgressHUD dismiss];
});
NSLog(@"%@",error.localizedDescription);
switch (error.code) {
case LAErrorSystemCancel:
{
NSLog(@"Authentication was cancelled by the system");
//切换到其他APP,系统取消验证Touch ID
break;
}
case LAErrorUserCancel:
{
NSLog(@"Authentication was cancelled by the user");
//用户取消验证Touch ID
break;
}
case LAErrorUserFallback:
{
NSLog(@"User selected to enter custom password");
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//用户选择其他验证方式,切换主线程处理
}];
break;
}
default:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//其他情况,切换主线程处理
}];
break;
}
}
}
}];
}
else
{
//不支持指纹识别,LOG出错误详情
switch (error.code) {
case LAErrorTouchIDNotEnrolled:
{
NSLog(@"TouchID is not enrolled");
[SVProgressHUD showWithStatus:@"设备不支持指纹解锁"];
break;
}
case LAErrorPasscodeNotSet:
{
[SVProgressHUD showWithStatus:@"设备未设置指纹解锁"];
NSLog(@"A passcode has not been set");
break;
}
default:
{
[SVProgressHUD showWithStatus:@"设备未设置touchId"];
NSLog(@"TouchID not available");
break;
}
}
NSLog(@"%@",error.localizedDescription);
}