iOS-Touch ID验证

废话不说,直接代码拿走:

.h文件

//  Copyright © 2017年 rjx. All rights reserved.
#import <Foundation/Foundation.h>

typedef enum : NSUInteger {
    TouchIDState_DEVICENOTSUPPORTED = 1011, //设备不支持
    TouchIDState_DEVICENOTSAFEGUARD = 1012, //设备未处于安全保护中
    TouchIDState_DEVICENOTREGUIDFIN = 1013,//设备没有注册过指纹
    TouchIDState_SUCCESSSUPPORT = 1000,//支持指纹识别
    TouchIDState_FINGETSUCCESS = 1002,//指纹识别成功
    TouchIDState_ERROR = 1001,//指纹识别失败
    TouchIDState_ChoosePassWord = 999,//选择输入密码
} TouchIDRecognitionState;


typedef void(^TouchIDRecognitionManagerBlock)(NSDictionary *callBackDic);

@interface TouchIDRecognitionManager : NSObject

/**
 回调 包含状态以及提示信息
 */
@property (nonatomic, copy) TouchIDRecognitionManagerBlock touchIDBlock;

/**
 * 指纹登录验证
 */
- (void)loadAuthentication;
/**
 监测指纹识别
 */
- (void)checkTouchIDPrint;
@end

.m文件

#import "TouchIDRecognitionManager.h"
#import <LocalAuthentication/LocalAuthentication.h>
@implementation TouchIDRecognitionManager



/**
 监测指纹识别
 */
- (void)checkTouchIDPrint
{
    LAContext *myContext = [[LAContext alloc] init];
    // 这个属性是设置指纹输入失败之后的弹出框的选项
    NSError *authError = nil;
    if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError])
    {
        NSDictionary *dic = @{
                              @"code" : @(TouchIDState_SUCCESSSUPPORT),
                              @"message" : @"支持指纹识别"
                              };
        [self runTouchIDStateBlockWith:dic];

    }else
    {
        NSDictionary * dic = [self CheckTouchIDStateBlockWith:authError message:@"" success:NO];
        [self runTouchIDStateBlockWith:dic];
    }
}

/**
 * 指纹登录验证
 */
- (void)loadAuthentication
{
    //系统支持,最低iOS 8.0
    if ([UIDevice currentDevice].systemVersion.floatValue < 8.0)
    {

        return;
    }
    __weak typeof(self) weakSelf = self;
    LAContext *myContext = [[LAContext alloc] init];
    // 这个属性是设置指纹输入失败之后的弹出框的选项
    myContext.localizedFallbackTitle = @"输入密码";
    NSError *authError = nil;
    NSString *myLocalizedReasonString = @"请按住Home键完成验证";
    // MARK: 判断设备是否支持指纹识别
    if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError])
    {
        [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:myLocalizedReasonString reply:^(BOOL success, NSError * _Nullable error) {

            NSDictionary *dic = [self CheckTouchIDStateBlockWith:error message:@"" success:success];
            [self runTouchIDStateBlockWith:dic];
        }];
    }
    else
    {
        DLog(@"设备不支持指纹");
        NSDictionary *dic = @{
                              @"code" : @(TouchIDState_DEVICENOTSUPPORTED),
                              @"message" : @"设备不支持指纹"
                              };
        [self runTouchIDStateBlockWith:dic];
    }
}

- (NSDictionary *)CheckTouchIDStateBlockWith:(NSError *)error message:(NSString *)message success:(BOOL)success
{
    TouchIDRecognitionState touchIdState = TouchIDState_DEVICENOTSUPPORTED;
    if(success)
    {
        touchIdState = TouchIDState_FINGETSUCCESS;
        message = @"指纹识别成功";
    }
    else
    {
        // 错误码
        switch (error.code)
        {
            case LAErrorAuthenticationFailed: // Authentication was not successful, because user failed to provide valid credentials
            {
                DLog(@"授权失败"); // -1 连续三次指纹识别错误
                touchIdState = TouchIDState_ERROR;
                message = @"授权失败";
            }
                break;
            case LAErrorUserCancel: // Authentication was canceled by user (e.g. tapped Cancel button)
            {
                DLog(@"用户取消验证Touch ID"); // -2 在TouchID对话框中点击了取消按钮
                touchIdState = TouchIDState_ERROR;
                message = @"用户取消验证";
            }
                break;
            case LAErrorUserFallback: // Authentication was canceled, because the user tapped the fallback button (Enter Password)
            {
                DLog(@"用户选择输入密码,切换主线程处理"); // -3 在TouchID对话框中点击了输入密码按钮
                touchIdState = TouchIDState_ChoosePassWord;
                message = @"用户选择输入密码";
            }
                break;
            case LAErrorSystemCancel: // Authentication was canceled by system (e.g. another application went to foreground)
            {
                DLog(@"取消授权,如其他应用切入,用户自主"); // -4 TouchID对话框被系统取消,例如按下Home或者电源键
                touchIdState = TouchIDState_ERROR;
                message = @"取消授权";
            }
                break;
            case LAErrorPasscodeNotSet: // Authentication could not start, because passcode is not set on the device.

            {
                DLog(@"设备系统未设置密码"); // -5
                touchIdState = TouchIDState_DEVICENOTSAFEGUARD;
                message = @"设备未处于安全保护中";
            }
                break;
            case LAErrorTouchIDNotAvailable: // Authentication could not start, because Touch ID is not available on the device
            {
                DLog(@"设备未设置Touch ID"); // -6
                touchIdState = TouchIDState_DEVICENOTREGUIDFIN;
                message = @"设备没有注册过指纹";
            }
                break;
            case LAErrorTouchIDNotEnrolled: // Authentication could not start, because Touch ID has no enrolled fingers
            {
                DLog(@"用户未录入指纹"); // -7
                touchIdState = TouchIDState_DEVICENOTREGUIDFIN;
                message = @"设备没有注册过指纹";
            }
                break;

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
            case LAErrorTouchIDLockout: //Authentication was not successful, because there were too many failed Touch ID attempts and Touch ID is now locked. Passcode is required to unlock Touch ID, e.g. evaluating LAPolicyDeviceOwnerAuthenticationWithBiometrics will ask for passcode as a prerequisite 用户连续多次进行Touch ID验证失败,Touch ID被锁,需要用户输入密码解锁,先Touch ID验证密码
            {
                DLog(@"Touch ID被锁,需要用户输入密码解锁"); // -8 连续五次指纹识别错误,TouchID功能被锁定,下一次需要输入系统密码
                touchIdState = TouchIDState_ERROR;
                message = @"Touch ID被锁,需要用户输入密码解锁";
            }
                break;
            case LAErrorAppCancel: // Authentication was canceled by application (e.g. invalidate was called while authentication was in progress) 如突然来了电话,电话应用进入前台,APP被挂起啦");
            {
                DLog(@"用户不能控制情况下APP被挂起"); // -9
                touchIdState = TouchIDState_ERROR;
                message = @"用户不能控制情况下APP被挂起";
            }
                break;
            case LAErrorInvalidContext: // LAContext passed to this call has been previously invalidated.
            {
                DLog(@"LAContext传递给这个调用之前已经失效"); // -10
                touchIdState = TouchIDState_ERROR;
                message = @"指纹识别失败";
            }
                break;
#else
#endif
            default:
            {
                touchIdState = TouchIDState_ERROR;
                message = @"指纹识别失败";
                break;
            }
        }
    }
    NSDictionary *dic = @{
                          @"code" : @(touchIdState),
                          @"message" : message,
                          };
    return dic;
}


/**
 执行回调
 */
- (void)runTouchIDStateBlockWith:(NSDictionary *)dic
{

    if (self.touchIDBlock) {
        self.touchIDBlock(dic);
    }
}


@end

需要注意的是:当对设备进行检测是否支持指纹识别的时候,如果检测失败,它会返回不一样的状态。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章