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

需要注意的是:當對設備進行檢測是否支持指紋識別的時候,如果檢測失敗,它會返回不一樣的狀態。

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