123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- //
- // JZTouchIDTool.m
- // JIZHANG
- //
- // Created by xiaoxi on 2017/12/20.
- // Copyright © 2017年 kuxuan. All rights reserved.
- //
- #import "JZTouchIDTool.h"
- #import <UIKit/UIKit.h>
- #import <LocalAuthentication/LocalAuthentication.h>
- /*
- * 验证成功
- */
- 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
|