指紋識别必須ios8 以上真機,面部識别據說同一套API 沒有支援面部識别的機器,沒有測試,直接貼代碼了,注釋比較清晰,關鍵點在于業務邏輯,由于指紋識别隻是類似授權自動登入,是以那些賬戶密碼。必須存儲,在授權成功自動登入,存儲密碼可參考keychain
ZbTouchManager.h
//
// ZbTouchManager.h
// TestFFmpegDemo
//
// Created by syStudio sy on 2019/4/22.
// Copyright © 2019 syStudio sy. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
typedef enum _LOCALTOUCHIDRESULT{
LOCALTOUCHID_NOT_SUPPORT = 0 ,/*裝置不支援*/
LOCALTOUCHID_SUCCESS,/*成功*/
LOCALTOUCHID_FAILD,/*失敗*/
LOCALTOUCHID_UserCancel,/*使用者取消*/
LOCALTOUCHID_UserFallback,/*使用者選擇輸入密碼*/
LOCALTOUCHID_SystemCancel,/*系統來電等導緻*/
LOCALTOUCHID_PasscodeNotSet,/*使用者沒有設定密碼*/
LOCALTOUCHID_NOT_Enrolled,/*使用者沒有設定touchID*/
LOCALTOUCHID_NOT_Available,/*使用者touchiD無效*/
LOCALTOUCHID_Other_Reason,/*其它原因*/
}LocalAuthent_result;
@interface ZbTouchManager : NSObject
+(id)sharedInstance;
-(int)checkTouchIDActionWithComplete:(void(^)(NSDictionary*resultDic,LocalAuthent_result result))callback;
@end
NS_ASSUME_NONNULL_END
ZbTouchManager.m
//
// ZbTouchManager.m
// TestFFmpegDemo
//
// Created by syStudio sy on 2019/4/22.
// Copyright © 2019 syStudio sy. All rights reserved.
//
#import "ZbTouchManager.h"
#import <LocalAuthentication/LocalAuthentication.h>
#define RetDicReasonKey @"ZbResultDscStr"
@implementation ZbTouchManager
static ZbTouchManager *instance = nil;
+(id)sharedInstance{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[ZbTouchManager alloc]init];
});
return instance;
}
-(int)checkTouchIDActionWithComplete:(void(^)(NSDictionary*resultDic,LocalAuthent_result result))callback{
NSMutableDictionary *retDic = [NSMutableDictionary dictionary];
if(NSFoundationVersionNumber < NSFoundationVersionNumber_iOS_8_0){
[retDic setValue:RetDicReasonKey forKey:@"版本低于8.0 不支援Touch ID"];
callback(retDic,LOCALTOUCHID_NOT_SUPPORT);
return LOCALTOUCHID_NOT_SUPPORT;
}
LAContext *context = [[LAContext alloc]init];
// 指紋認證錯誤後的第二個按鈕文字(不寫預設為“輸入密碼”)
context.localizedFallbackTitle = @"密碼";
if (@available(iOS 10.0.0,*)) {
context.localizedCancelTitle = @"取消";
}
NSError *error = nil;
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&error]) {
[context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:@"home 鍵指紋識别" reply:^(BOOL success, NSError * _Nullable error) {
if (success) {
dispatch_async(dispatch_get_main_queue(), ^{
[retDic setValue:@"指紋識别驗證成功" forKey:RetDicReasonKey];
callback(retDic,LOCALTOUCHID_SUCCESS);
});
}else if(error){
switch (error.code) {
case LAErrorAuthenticationFailed:
[retDic setValue:@"指紋識别驗證失敗" forKey:RetDicReasonKey];
callback(retDic,LOCALTOUCHID_FAILD);
break;
case LAErrorUserCancel:
[retDic setValue:@"使用者取消" forKey:RetDicReasonKey];
callback(retDic,LOCALTOUCHID_UserCancel);
break;
case LAErrorUserFallback:
[retDic setValue:@"使用者不使用TouchID 選擇搜懂輸入密碼" forKey:RetDicReasonKey];
callback(retDic,LOCALTOUCHID_UserFallback);
break;
case LAErrorSystemCancel:
[retDic setValue:@"系統取消 比如遇到來電,等" forKey:RetDicReasonKey];
callback(retDic,LOCALTOUCHID_SystemCancel);
break;
case LAErrorPasscodeNotSet:
[retDic setValue:@"使用者沒有設定密碼" forKey:RetDicReasonKey];
callback(retDic,LOCALTOUCHID_PasscodeNotSet);
break;
case LAErrorTouchIDNotEnrolled:
[retDic setValue:@"使用者沒有設定touchID" forKey:RetDicReasonKey];
callback(retDic,LOCALTOUCHID_NOT_Enrolled);
NSLog(@"使用者沒有設定touchID");
break;
case LAErrorTouchIDNotAvailable:
[retDic setValue:@"使用者touchiD 無效" forKey:RetDicReasonKey];
callback(retDic,LOCALTOUCHID_NOT_Available);
break;
default:
[retDic setValue:@"其它未知原因" forKey:RetDicReasonKey];
callback(retDic,LOCALTOUCHID_Other_Reason);
break;
}
}
}];
}else{
[retDic setValue:@"裝置不支援touchiID" forKey:RetDicReasonKey];
callback(retDic,LOCALTOUCHID_NOT_SUPPORT);
}
return LOCALTOUCHID_NOT_SUPPORT;
}
@end
參考文章