TouchID主要使用到LocalAuthentication框架,用于判断是否可以使用指纹解锁、是否录入了指纹,指纹是否正确。
下面主要讲封装一个工具类有两个方法,可以简单为你的APP加上TouchID这个功能。
检测TouchID是否可用,iPhone6之后才可使用
主要使用LAContext类,调用canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics方法判断是否支持TouchID,error用来返回不可用的原因。
+ (BOOL)isDeviceSupportTouchID
{
LAContext *context = [[LAContext alloc] init];
NSError *contextError = nil;
BOOL isSupport = [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&contextError];
if (isSupport) {
return YES;
}
else {
switch (contextError.code) {
case LAErrorTouchIDNotEnrolled:
{
//设备Touch ID不可用,用户未录入
NSLog(@"TouchID is not enrolled");
return NO;
break;
}
case LAErrorPasscodeNotSet:
{
//系统未设置密码
NSLog(@"A passcode has not been set");
return NO;
break;
}
default:
{
//TouchID 不可用,无指纹解锁功能
NSLog(@"TouchID not available");
return NO;
break;
}
}
}
}
开始调用指纹解锁的方法,replySuccess中进行成功的操作,failure中进行失败的操作。
+ (void)showTouchIDReplySuccess:(void(^)())replySuccess failure:(void(^)(NSError *error))failure
{
if([[[UIDevice currentDevice] systemVersion] floatValue] < 8){
failure(nil);
}
LAContext *context = [[LAContext alloc] init];
context.localizedFallbackTitle = @"验证登录密码";
//TODO:TOUCHID是否存在
NSError *contextError = nil;
if ([manger <span style="font-family: Arial, Helvetica, sans-serif;">isDeviceSupportTouchID</span>]) {
//TODO:TOUCHID开始运作
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"通过Home键验证已有指纹" reply:^(BOOL succes, NSError *error){
if(succes){
replySuccess();
}else{
failure(error);
DDLogError(@"识别错误:%@",error);
}
}];
}else{
failure(contextError);
}
[context release];
}