iOS_使用LAContext實現TouchID(指紋識別)的使用

在iPhone5s問世后,蘋果的后續移動設備相繼添加了指紋功能,從實際使用中還是相當方便的,比如快捷支付、快捷登錄等應用場景,系統也提供給了我們相關的操作框架:LocalAuthentication,使用LAContext對象即可完成指紋識別,由于指紋識別時遇到的情況也比較多,所以我做了相應的封裝,代碼具體如下:

提示:指紋識別必須用真機測試

-點擊下載工程

LM_TouchID.h 內容:

//
//  LM_TouchID.h
//  TouchID
//
//  Created by CoderDoctorLee on 16/6/16.
//  Copyright ? 2016年 CoderDoctorLee. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <LocalAuthentication/LocalAuthentication.h>

@protocol LM_TouchID_Delegate <NSObject>
//必須實現的兩個代理方法:
@required
/**
 *  @author CoderDoctorLee, 16-06-16 22:06:06
 *
 *  驗證成功
 */
- (void)LM_TouchID_AuthorizeSuccess;
/**
 *  @author CoderDoctorLee, 16-06-16 22:06:41
 *
 *  驗證失敗
 */
- (void)LM_TouchID_AuthorizeFailure;

//選擇實現的代理方法:
@optional

/**
 *  @author CoderDoctorLee, 16-06-16 22:06:12
 *
 *  取消了驗證(點擊了取消)
 */
- (void)LM_TouchID_AuthorizeUserCancel;

/**
 *  @author CoderDoctorLee, 16-06-16 22:06:58
 *
 *  在TouchID對話框點擊輸入密碼按鈕
 */
- (void)LM_TouchID_AuthorizeUserFallBack;

/**
 *  @author CoderDoctorLee, 16-06-16 22:06:58
 *
 *   在驗證的TouchID的過程中被系統取消 例如突然來電話、按了Home鍵、鎖屏...
 */
- (void)LM_TouchID_AuthorizeSystemCancel;

/**
 *  @author CoderDoctorLee, 16-06-16 22:06:18
 *
 *  無法使用TouchID,設備沒有設置密碼
 */
- (void)LM_TouchID_AuthorizePasswordNotSet;

/**
 *  @author CoderDoctorLee, 16-06-16 22:06:15
 *
 *  沒有錄入TouchID,無法使用
 */
- (void)LM_TouchID_AuthorizeTouchIDNotSet;

/**
 *  @author CoderDoctorLee, 16-06-16 22:06:19
 *
 *  該設備的TouchID無效
 */
- (void)LM_TouchID_AuthorizeTouchIDNotAvailable;

/**
 *  @author CoderDoctorLee, 16-06-16 22:06:17
 *
 *  多次連續使用Touch ID失敗,Touch ID被鎖,需要用戶輸入密碼解鎖
 */
- (void)LM_TouchID_AuthorizeTouchIDNotLockOut;

/**
 *  @author CoderDoctorLee, 16-06-16 22:06:47
 *
 *  當前軟件被掛起取消了授權(如突然來了電話,應用進入前臺)
 */
- (void)LM_TouchID_AuthorizeTouchIDAppCancel;

/**
 *  @author CoderDoctorLee, 16-06-16 22:06:45
 *
 *  當前軟件被掛起取消了授權 (授權過程中,LAContext對象被釋)
 */
- (void)LM_TouchID_AuthorizeTouchIDInvalidContext;

/**
 *  @author CoderDoctorLee, 16-06-16 22:06:29
 *
 *  當前設備不支持指紋識別
 */
- (void)LM_TouchID_AuthorizeNotSupport;
@end

@interface LM_TouchID : LAContext
@property (nonatomic, assign) id<LM_TouchID_Delegate> delegate;
/**
 *  @author CoderDoctorLee, 16-06-16 22:06:51
 *
 *  發起指紋驗證:
 */
- (void)startLM_TouchID_WithMessage:(NSString *)message FallBackTitle:(NSString *)fallBackTitle Delegate:(id<LM_TouchID_Delegate>)delegate;
@end

LM_TouchID.m 內容:

//
//  LM_TouchID.m
//  TouchID
//
//  Created by CoderDoctorLee on 16/6/16.
//  Copyright ? 2016年 CoderDoctorLee. All rights reserved.
//

#import "LM_TouchID.h"

@implementation LM_TouchID
- (void)startLM_TouchID_WithMessage:(NSString *)message FallBackTitle:(NSString *)fallBackTitle Delegate:(id<LM_TouchID_Delegate>)delegate
{
    LAContext *context = [[LAContext alloc] init];
    context.localizedFallbackTitle = fallBackTitle;
    
    NSError *error = nil;
    self.delegate = delegate;
    //判斷代理人是否為空
    if (self.delegate != nil) {
        if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
            //使用context對象對識別的情況進行評估
            [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:message reply:^(BOOL success, NSError * _Nullable error) {
                //識別成功:
                if (success) {
                    if ([self.delegate respondsToSelector:@selector(LM_TouchID_AuthorizeSuccess)]) {
                        //必須回到主線程執行,否則在更新UI時會出錯!以下相同
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            [self.delegate LM_TouchID_AuthorizeSuccess];
                        }];
                    }
                }
                //識別失敗(對應代理方法的每種情況,不實現對應方法就沒有反應)
                else if (error)
                {
                    switch (error.code) {
                        case LAErrorAuthenticationFailed:{
                            if ([self.delegate respondsToSelector:@selector(LM_TouchID_AuthorizeFailure)]) {
                                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                [self.delegate LM_TouchID_AuthorizeFailure];
                                }];
                            }
                            break;
                        }
                        case LAErrorUserCancel:{
                            if ([self.delegate respondsToSelector:@selector(LM_TouchID_AuthorizeUserCancel)]) {
                                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                [self.delegate LM_TouchID_AuthorizeUserCancel];
                                }];
                            }
                            break;
                        }
                        case LAErrorUserFallback:{
                            if ([self.delegate respondsToSelector:@selector(LM_TouchID_AuthorizeUserFallBack)]) {
                                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                [self.delegate LM_TouchID_AuthorizeUserFallBack];
                                }];
                            }
                            break;
                        }
                        case LAErrorSystemCancel:{
                            if ([self.delegate respondsToSelector:@selector(LM_TouchID_AuthorizeSystemCancel)]) {
                                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                [self.delegate LM_TouchID_AuthorizeSystemCancel];
                                }];
                            }
                            break;
                        }
                        case LAErrorTouchIDNotEnrolled:
                        {
                            if ([self.delegate respondsToSelector:@selector(LM_TouchID_AuthorizeTouchIDNotSet)]) {
                                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                [self.delegate LM_TouchID_AuthorizeTouchIDNotSet];
                                }];
                            }
                            break;
                        }
                        case LAErrorPasscodeNotSet:{
                            if ([self.delegate respondsToSelector:@selector(LM_TouchID_AuthorizePasswordNotSet)]) {
                                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                [self.delegate LM_TouchID_AuthorizePasswordNotSet];
                                }];
                            }
                            break;
                        }
                        case LAErrorTouchIDNotAvailable:{
                            if ([self.delegate respondsToSelector:@selector(LM_TouchID_AuthorizeTouchIDNotAvailable)]) {
                                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                    [self.delegate LM_TouchID_AuthorizeTouchIDNotAvailable];
                                }];
                            }
                            break;
                        }
                        case LAErrorTouchIDLockout:{
                            if ([self.delegate respondsToSelector:@selector(LM_TouchID_AuthorizeTouchIDNotLockOut)]) {
                                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                    [self.delegate LM_TouchID_AuthorizeTouchIDNotLockOut];
                                }];
                            }
                            break;
                        }
                        case LAErrorAppCancel:{
                            if ([self.delegate respondsToSelector:@selector(LM_TouchID_AuthorizeTouchIDAppCancel)]) {
                                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                    [self.delegate LM_TouchID_AuthorizeTouchIDAppCancel];
                                }];
                            }
                            break;
                        }
                        case LAErrorInvalidContext:{
                            if ([self.delegate respondsToSelector:@selector(LM_TouchID_AuthorizeTouchIDInvalidContext)]) {
                                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                    [self.delegate LM_TouchID_AuthorizeTouchIDInvalidContext];
                                }];
                            }
                            break;
                        }
                        default:
                            break;
                    }
                }
            }];
        }
    }
    //設備不支持指紋識別
    else
    {
        if ([self.delegate respondsToSelector:@selector(LM_TouchID_AuthorizeNotSupport)]) {
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [self.delegate LM_TouchID_AuthorizeNotSupport];
            }];
        }
    }
}
@end

在ViewController里使用示例:

//
//  ViewController.m
//  TouchID
//
//  Created by CoderDoctorLee on 16/6/17.
//  Copyright ? 2016年 CoderDoctorLee. All rights reserved.
//

#import "ViewController.h"
#import "LM_TouchID.h"
@interface ViewController ()<LM_TouchID_Delegate>
@property(nonatomic, strong)UILabel *label;
@property(nonatomic, strong)LM_TouchID *touchID;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //用來顯示驗證信息
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 175, 50)];
    _label.backgroundColor = [UIColor blueColor];
    _label.textColor = [UIColor whiteColor];
    [self.view addSubview:_label];
    
    self.touchID = [[LM_TouchID alloc] init];
    //開始驗證:
    [_touchID startLM_TouchID_WithMessage:@"請驗證指紋轉賬100萬" FallBackTitle:@"輸入密碼" Delegate:self];
}
- (void)LM_TouchID_AuthorizeSuccess
{
    _label.text = @"驗證成功";
}
- (void)LM_TouchID_AuthorizeFailure
{
    _label.text = @"驗證失敗";
}
- (void)LM_TouchID_AuthorizeNotSupport
{
    _label.text = @"設備不支持";
}
- (void)LM_TouchID_AuthorizeUserCancel
{
    _label.text = @"用戶取消了驗證";
}
//其他代理方法就不一一實現了,大家可以自行測驗
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容