// // JZTouchIDTool.m // JIZHANG // // Created by xiaoxi on 2017/12/20. // Copyright © 2017年 kuxuan. All rights reserved. // #import "JZTouchIDTool.h" #import #import /* * 验证成功 */ NSString *const JZValidateTouchIDSuccess = @"JZValidateTouchIDSuccess"; /* * 验证失败 */ NSString *const JZValidateTouchIDFailure = @"JZValidateTouchIDFailure"; /* * 取消验证 */ NSString *const JZValidateTouchIDCancel = @"JZValidateTouchIDCancel"; /* * 输入密码 */ NSString *const JZValidateTouchIDUserFallback = @"JZValidateTouchIDUserFallback"; /* * 设备未设置密码 */ NSString *const JZValidateTouchIDPasscodeNotSet = @"JZValidateTouchIDPasscodeNotSet"; /* * 设备指纹不可用 */ NSString *const JZValidateTouchIDNotAvailable = @"JZValidateTouchIDNotAvailable"; /* * 设备未设置指纹 */ NSString *const JZValidateTouchIDNotEnrolled = @"JZValidateTouchIDNotEnrolled"; /* * 设备指纹被锁定 */ NSString *const JZValidateTouchIDLockout = @"JZValidateTouchIDLockout"; /* * 验证指纹被禁止 */ NSString *const JZValidateTouchIDInvalidContext = @"JZValidateTouchIDInvalidContext"; @implementation JZTouchIDTool + (void)setNeedTouchIDYes { [[NSUserDefaults standardUserDefaults] setBool:YES forKey:JZNEEDTOUCHID]; } + (void)setNeedTouchIDNo { [[NSUserDefaults standardUserDefaults] setBool:NO forKey:JZNEEDTOUCHID]; } + (BOOL)needTouchID { return [[NSUserDefaults standardUserDefaults] boolForKey:JZNEEDTOUCHID]; } + (void)openTouchID { [[NSUserDefaults standardUserDefaults] setBool:YES forKey:JZOPENTOUCHID]; } + (void)closeTouchID { [[NSUserDefaults standardUserDefaults] setBool:NO forKey:JZOPENTOUCHID]; } + (BOOL)isOpenTouchID { return [[NSUserDefaults standardUserDefaults] boolForKey:JZOPENTOUCHID]; } + (BOOL)supportTouchID { if (@available(iOS 8.0, *)) { return YES; } else { return NO; } } + (BOOL)canTouchID { if (@available(iOS 8.0, *)) { // LAContext *context = [[LAContext alloc] init]; // NSError *error = nil; // [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]; // if (error.code == LAErrorPasscodeNotSet || error.code == LAErrorTouchIDNotEnrolled || error.code == LAErrorTouchIDNotAvailable) { // return NO; // } return YES; } else { return NO; } } + (void)validateTouchID { if (@available(iOS 8.0, *)) { LAContext *context = [[LAContext alloc] init]; context.localizedFallbackTitle = @""; NSError *error = nil; if (@available(iOS 9.0, *)) { [context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:@"指纹解锁" reply:^(BOOL success, NSError * _Nullable error) { dispatch_async(dispatch_get_main_queue(), ^{ if (error) { switch (error.code) { case LAErrorAuthenticationFailed: [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDFailure object:nil]; break; case LAErrorUserCancel: [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDCancel object:nil]; break; case LAErrorUserFallback: [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDUserFallback object:nil]; break; case LAErrorSystemCancel: [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDCancel object:nil]; break; case LAErrorPasscodeNotSet: [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDPasscodeNotSet object:nil]; break; case LAErrorTouchIDNotAvailable: [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDNotAvailable object:nil]; break; case LAErrorTouchIDNotEnrolled: [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDNotEnrolled object:nil]; break; case LAErrorTouchIDLockout: { [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDLockout object:nil]; // [self validateTouchID]; } break; case LAErrorAppCancel: [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDCancel object:nil]; break; case LAErrorInvalidContext: { [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDInvalidContext object:nil]; // [self validateTouchID]; } break; default: break; } } else { [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDSuccess object:nil]; } }); }]; } else { [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"指纹解锁" reply:^(BOOL success, NSError * _Nullable error) { dispatch_async(dispatch_get_main_queue(), ^{ if (error) { switch (error.code) { case LAErrorAuthenticationFailed: [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDFailure object:nil]; break; case LAErrorUserCancel: [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDCancel object:nil]; break; case LAErrorUserFallback: [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDUserFallback object:nil]; break; case LAErrorSystemCancel: [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDCancel object:nil]; break; case LAErrorPasscodeNotSet: [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDPasscodeNotSet object:nil]; break; case LAErrorTouchIDNotAvailable: [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDNotAvailable object:nil]; break; case LAErrorTouchIDNotEnrolled: [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDNotEnrolled object:nil]; break; default: break; } } else { [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDSuccess object:nil]; } }); }]; } } } @end