Skip to content

Commit

Permalink
指纹/面容验证主类
Browse files Browse the repository at this point in the history
  • Loading branch information
micyo202 committed Dec 22, 2017
1 parent d935a32 commit b1bde8b
Show file tree
Hide file tree
Showing 2 changed files with 315 additions and 0 deletions.
89 changes: 89 additions & 0 deletions YZAuthID/YZAuthID.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/************************************************************
Class : YZAuthID.m
Describe : TouchID/FaceID认证方法
Company : Prient
Author : Yanzheng
Date : 2017-12-22
Version : 1.0
Declare : Copyright © 2017 Yanzheng. All rights reserved.
************************************************************/

#import <LocalAuthentication/LocalAuthentication.h>

/**
* TouchID 状态
*/
typedef NS_ENUM(NSUInteger, YZAuthIDState){

/**
* 当前设备不支持TouchID/FaceID
*/
YZAuthIDStateNotSupport = 0,
/**
* TouchID/FaceID 验证成功
*/
YZAuthIDStateSuccess = 1,

/**
* TouchID/FaceID 验证失败
*/
YZAuthIDStateFail = 2,
/**
* TouchID/FaceID 被用户手动取消
*/
YZAuthIDStateUserCancel = 3,
/**
* 用户不使用TouchID/FaceID,选择手动输入密码
*/
YZAuthIDStateInputPassword = 4,
/**
* TouchID/FaceID 被系统取消 (如遇到来电,锁屏,按了Home键等)
*/
YZAuthIDStateSystemCancel = 5,
/**
* TouchID/FaceID 无法启动,因为用户没有设置密码
*/
YZAuthIDStatePasswordNotSet = 6,
/**
* TouchID/FaceID 无法启动,因为用户没有设置TouchID
*/
YZAuthIDStateTouchIDNotSet = 7,
/**
* TouchID/FaceID 无效
*/
YZAuthIDStateTouchIDNotAvailable = 8,
/**
* TouchID/FaceID 被锁定(连续多次验证TouchID/FaceID失败,系统需要用户手动输入密码)
*/
YZAuthIDStateTouchIDLockout = 9,
/**
* 当前软件被挂起并取消了授权 (如App进入了后台等)
*/
YZAuthIDStateAppCancel = 10,
/**
* 当前软件被挂起并取消了授权 (LAContext对象无效)
*/
YZAuthIDStateInvalidContext = 11,
/**
* 系统版本不支持TouchID/FaceID (必须高于iOS 8.0才能使用)
*/
YZAuthIDStateVersionNotSupport = 12
};



@interface YZAuthID : LAContext

typedef void (^StateBlock)(YZAuthIDState state,NSError *error);


/**
启动TouchID/FaceID进行验证
@param desc TouchID/FaceID显示的描述
@param block 回调状态的block
*/

- (void)yz_showAuthIDWithDescribe:(NSString *)desc BlockState:(StateBlock)block;

@end
226 changes: 226 additions & 0 deletions YZAuthID/YZAuthID.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
/************************************************************
Class : YZAuthID.m
Describe : TouchID/FaceID认证方法
Company : Prient
Author : Yanzheng
Date : 2017-12-22
Version : 1.0
Declare : Copyright © 2017 Yanzheng. All rights reserved.
************************************************************/

#import "YZAuthID.h"
#import <UIKit/UIKit.h>

@implementation YZAuthID

+ (instancetype)sharedInstance {
static YZAuthID *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[YZAuthID alloc] init];
});
return instance;
}

- (void)yz_showAuthIDWithDescribe:(NSString *)desc BlockState:(StateBlock)block{
if (NSFoundationVersionNumber < NSFoundationVersionNumber_iOS_8_0) {

dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"系统版本不支持TouchID/FaceID (必须高于iOS 8.0才能使用)");
block(YZAuthIDStateVersionNotSupport,nil);
});

return;
}

LAContext *context = [[LAContext alloc] init];

// 认证失败提示信息,为 @"" 则不提示
context.localizedFallbackTitle = @"输入密码";

NSError *error = nil;

// LAPolicyDeviceOwnerAuthenticationWithBiometrics: 用TouchID/FaceID验证
// LAPolicyDeviceOwnerAuthentication: 用TouchID/FaceID或密码验证, 默认是错误两次或锁定后, 弹出输入密码界面(本案例使用)
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&error]) {

[context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:desc == nil ? @"通过Home键验证已有指纹" : desc reply:^(BOOL success, NSError * _Nullable error) {

if (success) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID/FaceID 验证成功");
block(YZAuthIDStateSuccess,error);
});
}else if(error){

if (@available(iOS 11.0, *)) {
switch (error.code) {
case LAErrorAuthenticationFailed:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID/FaceID 验证失败");
block(YZAuthIDStateFail,error);
});
break;
}
case LAErrorUserCancel:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID/FaceID 被用户手动取消");
block(YZAuthIDStateUserCancel,error);
});
}
break;
case LAErrorUserFallback:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"用户不使用TouchID/FaceID,选择手动输入密码");
block(YZAuthIDStateInputPassword,error);
});
}
break;
case LAErrorSystemCancel:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID/FaceID 被系统取消 (如遇到来电,锁屏,按了Home键等)");
block(YZAuthIDStateSystemCancel,error);
});
}
break;
case LAErrorPasscodeNotSet:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID/FaceID 无法启动,因为用户没有设置密码");
block(YZAuthIDStatePasswordNotSet,error);
});
}
break;
//case LAErrorTouchIDNotEnrolled:{
case LAErrorBiometryNotEnrolled:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID/FaceID 无法启动,因为用户没有设置TouchID");
block(YZAuthIDStateTouchIDNotSet,error);
});
}
break;
//case LAErrorTouchIDNotAvailable:{
case LAErrorBiometryNotAvailable:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID 无效");
block(YZAuthIDStateTouchIDNotAvailable,error);
});
}
break;
//case LAErrorTouchIDLockout:{
case LAErrorBiometryLockout:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID 被锁定(连续多次验证TouchID/FaceID失败,系统需要用户手动输入密码)");
block(YZAuthIDStateTouchIDLockout,error);
});
}
break;
case LAErrorAppCancel:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"当前软件被挂起并取消了授权 (如App进入了后台等)");
block(YZAuthIDStateAppCancel,error);
});
}
break;
case LAErrorInvalidContext:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"当前软件被挂起并取消了授权 (LAContext对象无效)");
block(YZAuthIDStateInvalidContext,error);
});
}
break;
default:
break;
}
} else {
// iOS 11.0以下的版本只有 TouchID 认证
switch (error.code) {
case LAErrorAuthenticationFailed:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID 验证失败");
block(YZAuthIDStateFail,error);
});
break;
}
case LAErrorUserCancel:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID 被用户手动取消");
block(YZAuthIDStateUserCancel,error);
});
}
break;
case LAErrorUserFallback:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"用户不使用TouchID,选择手动输入密码");
block(YZAuthIDStateInputPassword,error);
});
}
break;
case LAErrorSystemCancel:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID 被系统取消 (如遇到来电,锁屏,按了Home键等)");
block(YZAuthIDStateSystemCancel,error);
});
}
break;
case LAErrorPasscodeNotSet:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID 无法启动,因为用户没有设置密码");
block(YZAuthIDStatePasswordNotSet,error);
});
}
break;
case LAErrorTouchIDNotEnrolled:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID 无法启动,因为用户没有设置TouchID");
block(YZAuthIDStateTouchIDNotSet,error);
});
}
break;
//case :{
case LAErrorTouchIDNotAvailable:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID 无效");
block(YZAuthIDStateTouchIDNotAvailable,error);
});
}
break;
case LAErrorTouchIDLockout:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID 被锁定(连续多次验证TouchID失败,系统需要用户手动输入密码)");
block(YZAuthIDStateTouchIDLockout,error);
});
}
break;
case LAErrorAppCancel:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"当前软件被挂起并取消了授权 (如App进入了后台等)");
block(YZAuthIDStateAppCancel,error);
});
}
break;
case LAErrorInvalidContext:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"当前软件被挂起并取消了授权 (LAContext对象无效)");
block(YZAuthIDStateInvalidContext,error);
});
}
break;
default:
break;
}
}

}
}];

}else{

dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"当前设备不支持TouchID/FaceID");
block(YZAuthIDStateNotSupport,error);
});

}
}

@end

0 comments on commit b1bde8b

Please sign in to comment.