本文仅实现简单的验证跳转
引入依赖库
#import
指纹验证的实现代码如下:
- (void)authenticateUser
{
//初始化上下文对象
LAContext* context = [[LAContext alloc] init];
//错误对象
NSError* error = nil;
NSString* result = @"Authentication is needed to access your notes.";
//首先使用canEvaluatePolicy 判断设备支持状态
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
//支持指纹验证
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {
if (success) {
//验证成功,主线程处理UI
// [self showAlertWithTitle:@"验证成功跳转" Block:^{
UIViewController *viewC = [[UIViewController alloc]init];
viewC.view.backgroundColor = [UIColor redColor];
[self presentViewController:viewC animated:YES completion:nil];
// }];
}
else
{
NSLog(@"%@",error.localizedDescription);
switch (error.code) {
case LAErrorSystemCancel:
{
NSLog(@"Authentication was cancelled by the system");
//切换到其他APP,系统取消验证Touch ID
[self showAlertWithTitle:@"系统取消授权" Block:^{
}];
break;
}
case LAErrorUserCancel:
{
NSLog(@"Authentication was cancelled by the user");
//用户取消验证Touch ID
// [self showAlertWithTitle:@"用户取消TouchID授权" Block:^{
//
// }];
break;
}
case LAErrorUserFallback:
{
NSLog(@"User selected to enter custom password");
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//用户选择其他验证方式,切换主线程处理
[self alertAction];
}];
break;
}
default:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//其他情况,切换主线程处理
[self showAlertWithTitle:@"验证超过限制次数" Block:^{
}];
}];
break;
}
}
}
}];
}
else
{
//不支持指纹识别,LOG出错误详情
switch (error.code) {
case LAErrorTouchIDNotEnrolled:
{
NSLog(@"TouchID is not enrolled");
break;
}
case LAErrorPasscodeNotSet:
{
NSLog(@"A passcode has not been set");
break;
}
default:
{
NSLog(@"TouchID not available");
break;
}
}
NSLog(@"%@",error.localizedDescription);
// [self showPasswordAlert];
}
}
// 弹窗事件
- (void)alertAction{
NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"输入密码" message:@"蒋永昌" preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
// Listen for changes to the text field's text so that we can toggle the current
// action's enabled property based on whether the user has entered a sufficiently
// secure entry.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];
textField.secureTextEntry = YES;
}];
// Create the actions.
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"The \"Okay/Cancel\" alert's cancel action occured.");
// Stop listening for text changed notifications.
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
}];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"The \"Okay/Cancel\" alert's other action occured.");
if ([alertController.textFields.firstObject.text isEqualToString:@"123456"]) {
UIViewController *viewC = [[UIViewController alloc]init];
viewC.view.backgroundColor = [UIColor redColor];
[self presentViewController:viewC animated:YES completion:nil];
}
// Stop listening for text changed notifications.
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
}];
otherAction.enabled = NO;
// Hold onto the secure text alert action to toggle the enabled/disabled state when the text changed.
self.secureTextAlertAction = otherAction;
// Add the actions.
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[self presentViewController:alertController animated:YES completion:nil];
}
// 监听输入框中字符数
- (void)handleTextFieldTextDidChangeNotification:(NSNotification *)notification {
UITextField *textField = notification.object;
// Enforce a minimum length of >= 5 characters for secure text alerts.
self.secureTextAlertAction.enabled = textField.text.length >= 6;
}
指纹识别识别的几种反馈
/**
typedef NS_ENUM(NSInteger, LAError)
{
//授权失败
LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,
//用户取消Touch ID授权
LAErrorUserCancel = kLAErrorUserCancel,
//用户选择输入密码
LAErrorUserFallback = kLAErrorUserFallback,
//系统取消授权(例如其他APP切入)
LAErrorSystemCancel = kLAErrorSystemCancel,
//系统未设置密码
LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet,
//设备Touch ID不可用,例如未打开
LAErrorTouchIDNotAvailable = kLAErrorTouchIDNotAvailable,
//设备Touch ID不可用,用户未录入
LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled,
} NS_ENUM_AVAILABLE(10_10, 8_0);
*/
Demo下载地址
参考文章:
iOS开发之提示框的使用UIAlertController
iOS开发之指纹解锁