在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(, , , )];
_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