iOS LAPolicy指纹解锁
2018-05-09 本文已影响17人
倪大头
LAPolicy是一个枚举,它提供两个值:LAPolicyDeviceOwnerAuthenticationWithBiometrics和LAPolicyDeviceOwnerAuthentication.
LAPolicyDeviceOwnerAuthenticationWithBiometrics支持iOS8以上系统,LAPolicyDeviceOwnerAuthentication支持iOS9以上系统,LAPolicyDeviceOwnerAuthentication同时支持TouchID和设备密码认证。
使用需导入:
#import <LocalAuthentication/LocalAuthentication.h>
//判断系统版本
if (NSFoundationVersionNumber < NSFoundationVersionNumber_iOS_8_0) {
[self showRequestError:self.view withReason:@"系统版本不支持TouchID"];
return;
}
LAContext *context = [[LAContext alloc]init];
context.localizedFallbackTitle = @"输入密码";
NSError *error = nil;
//判断是否可以使用TouchID
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"Home键验证已有手机指纹" reply:^(BOOL success, NSError * _Nullable error) {
if (success) {
dispatch_async(dispatch_get_main_queue(), ^{
messageLabel.text = @"解锁成功";
});
}else {
switch (error.code) {
case LAErrorAuthenticationFailed:
{
dispatch_async(dispatch_get_main_queue(), ^{
messageLabel.text = @"验证失败";
});
break;
}
case LAErrorUserCancel:
{
dispatch_async(dispatch_get_main_queue(), ^{
messageLabel.text = @"用户取消验证";
});
break;
}
case LAErrorUserFallback:
{
dispatch_async(dispatch_get_main_queue(), ^{
messageLabel.text = @"用户选择手动输入密码";
});
break;
}
case LAErrorSystemCancel:
{
dispatch_async(dispatch_get_main_queue(), ^{
messageLabel.text = @"被系统取消(如遇来电,锁屏,按了Home键等)";
});
break;
}
case LAErrorPasscodeNotSet:
{
dispatch_async(dispatch_get_main_queue(), ^{
messageLabel.text = @"用户未设置密码";
});
break;
}
case LAErrorTouchIDNotEnrolled:
{
dispatch_async(dispatch_get_main_queue(), ^{
messageLabel.text = @"用户未设置TouchID";
});
break;
}
case LAErrorTouchIDNotAvailable:
{
dispatch_async(dispatch_get_main_queue(), ^{
messageLabel.text = @"TouchID无效";
});
break;
}
case LAErrorTouchIDLockout:
{
dispatch_async(dispatch_get_main_queue(), ^{
messageLabel.text = @"多次验证失败,TouchID被锁定";
});
break;
}
case LAErrorAppCancel:
{
dispatch_async(dispatch_get_main_queue(), ^{
messageLabel.text = @"被APP取消(如切换到后台)";
});
break;
}
case LAErrorInvalidContext:
{
dispatch_async(dispatch_get_main_queue(), ^{
messageLabel.text = @"LAContext对象无效";
});
break;
}
default:
break;
}
}
}];
}else {
messageLabel.text = @"当前设备不支持TouchID";
}