ios --指纹TouchID
最近在做一套登录系统,包括指纹登录、手势登录以及账号密码登录,在此简单记录一下指纹的处理逻辑。
// Created by ever on 17/3/7.
// Copyright © 2017年 ever. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger, TITouchIDVerificationError) {
TITouchIDVerificationErrorLocalizedReason,
TITouchIDVerificationErrorAuthFailMessage,
TITouchIDVerificationErrorNotAvailableMessage,
TITouchIDVerificationErrorNotEnrolledMessage,
TITouchIDVerificationErrorLockOut,
TITouchIDVerificationErrorPasscodeNotSetMessage,
TITouchIDVerificationErrorUserCancel,
TITouchIDVerificationErrorAppCancel,
TITouchIDVerificationErrorUnknown
}; typedef NS_ENUM(NSInteger, TIVerificationStatus) {
TIVerificationStatusSuccess,
TIVerificationStatusPasswordNotSet,
TIVerificationStatusNotEncrolled,
TIVerificationStatusFailed,
TIVerificationStatusFallback,
TIVerificationStatusLockOut,
TIVerificationStatusCancel,
TIVerificationStatusError,
}; typedef void(^VerificationCompletion)(TIVerificationStatus status, NSError *error); @interface LYYTouchID : NSObject
/**
调用TouchID @param completion 验证完成,如果返回YES,验证成功;如果返回NO,验证失败(输入密码和指纹验证失败)
*/
+ (void)touchVerificationWithFallbackTitle:(NSString*)fallbackTitle completion:(VerificationCompletion)completion; @end
// Created by ever on 17/3/7.
// Copyright © 2017年 ever. All rights reserved. #import "LYYTouchID.h"
#import <UIKit/UIKit.h>
#import <LocalAuthentication/LocalAuthentication.h> #define TI_IS_iOS_LATER(num) ([UIDevice currentDevice].systemVersion.floatValue >= (num))
#define TI_IS_iOS8_LATER TI_IS_iOS_LATER(8.0)
#define TI_IS_iOS9_LATER TI_IS_iOS_LATER(9.0)
#define TI_IS_iOS10_LATER TI_IS_iOS_LATER(10.0)
#define TI_IS_iOS11_LATER TI_IS_iOS_LATER(11.0) NSString * const TouchIDLocalizedReason = @"请您验证已有TouchID";
NSString * const TouchIDNotAvailableMessage = @"您当前设备不支持TouchID";
NSString * const TouchIDNotEnrolledMessage = @"您当前没有录入指纹信息";
NSString * const TouchIDNotCorrectMessage = @"您的指纹信息不匹配";
NSString * const TouchIDPasscodeNotSetMessage = @"您当前没有设置密码";
NSString * const TouchIDCancelMessage = @"您已取消验证TouchID";
NSString * const TouchIDUnknownErrorMessage = @"出现未知错误";
NSString * const TouchIDLockOutMessage = @"TouchID 被锁定"; static BOOL _isTouchIDSecondCheck;//用于存储第一遍canEvaluatePolicy检查到的指纹被锁的状态
static BOOL _touchIDSecondCheck;//用于存储canEvaluatePolicy是否是第二次查询 @interface LYYTouchID ()
@end @implementation LYYTouchID #pragma mark - 验证
+ (void)touchVerificationWithFallbackTitle:(NSString*)fallbackTitle completion:(VerificationCompletion)completion{
LAContext *context = [[LAContext alloc] init];
context.localizedFallbackTitle = fallbackTitle?:@"";
LAPolicy policy = [self setPolicy]; NSError *notSupportError = nil;
if ([context canEvaluatePolicy:policy error:¬SupportError]) { //处理10.*系统纯指纹策略不走passwordNotSet方法
if (_touchIDSecondCheck&&TI_IS_iOS10_LATER&&!TI_IS_iOS11_LATER) {//第二次检测指纹是否可用(指纹+密码策略),且可以
_touchIDSecondCheck = NO;
completion(TIVerificationStatusNotEncrolled,[self returnErrorInfoWithErrorCode:LAErrorTouchIDNotEnrolled]);
return;
} //不是第二次检测指纹是否可用,直接进行验证
[context evaluatePolicy:policy localizedReason:TouchIDLocalizedReason reply:^(BOOL success, NSError * _Nullable error) { dispatch_async(dispatch_get_main_queue(), ^{
if (success) { if (TI_IS_iOS9_LATER&&_isTouchIDSecondCheck) {//TouchID 解锁成功时,更改为纯指纹策略,验证TouchID
_isTouchIDSecondCheck = NO;
[self touchVerificationWithFallbackTitle:fallbackTitle completion:completion];
}else{//ios9.0之前版本不支持密码策略直接返回信息
_isTouchIDSecondCheck = NO;
completion(TIVerificationStatusSuccess,nil);
}
}
else {
int code = (int)error.code;
////设置锁定状态为NO
_isTouchIDSecondCheck = NO;
[self dealErrorWithErrorCode:code fallbackTitle:fallbackTitle complection:completion];
} }); }];
}else { [self dealErrorWithErrorCode:(int)notSupportError.code fallbackTitle:fallbackTitle complection:completion];
}
}
#pragma mark - 设置指纹策略
+ (LAPolicy)setPolicy{
LAPolicy policy; if (_touchIDSecondCheck&&TI_IS_iOS9_LATER){//已经查验过纯指纹策略,且纯指纹策略不支持,改变策略为指纹+密码进行二次验证
policy = LAPolicyDeviceOwnerAuthentication;
return policy;
}
if (_isTouchIDSecondCheck&&TI_IS_iOS9_LATER) {//指纹被锁定且系统大于9.0,使用“指纹+密码”策略
policy = LAPolicyDeviceOwnerAuthentication;
}else{//使用“指纹”策略(默认是指纹策略)
policy = LAPolicyDeviceOwnerAuthenticationWithBiometrics;
}
return policy;
}
#pragma mark - 处理不同错误
+ (void)dealErrorWithErrorCode:(int)code fallbackTitle:(NSString*)fallbackTitle complection:(VerificationCompletion)completion{ switch (code) {
case LAErrorUserFallback:
completion(TIVerificationStatusFallback,[self returnErrorInfoWithErrorCode:code]);
break; case LAErrorAuthenticationFailed:
completion(TIVerificationStatusFailed,[self returnErrorInfoWithErrorCode:code]);
break; case LAErrorSystemCancel:{
_isTouchIDSecondCheck = NO;
_touchIDSecondCheck = NO;
}
break; case LAErrorPasscodeNotSet:
_isTouchIDSecondCheck = NO;
_touchIDSecondCheck = NO;
completion(TIVerificationStatusPasswordNotSet,[self returnErrorInfoWithErrorCode:code]);
break; case LAErrorTouchIDNotAvailable: break; case LAErrorAppCancel:{
_isTouchIDSecondCheck = NO;
_touchIDSecondCheck = NO;
}
break;
case LAErrorUserCancel:
_isTouchIDSecondCheck = NO;
_touchIDSecondCheck = NO;
completion(TIVerificationStatusCancel,[self returnErrorInfoWithErrorCode:code]);
break; default: {
if (TI_IS_iOS9_LATER) {
if (code==LAErrorTouchIDLockout) {
//ios9.0更改为密码策略,解锁TouchID
_isTouchIDSecondCheck = YES;
[self touchVerificationWithFallbackTitle:fallbackTitle completion:completion];
break; }
}
if (TI_IS_iOS8_LATER&&!TI_IS_iOS11_LATER) {
if (code==LAErrorTouchIDNotEnrolled) {
//处理ios 10.*系统不走passwordNotSet
if (TI_IS_iOS10_LATER&&!_touchIDSecondCheck) {//ios 9.0之后,第一次返回指纹不可用的结果(这时是纯指纹策略)
_touchIDSecondCheck = YES;
[self touchVerificationWithFallbackTitle:fallbackTitle completion:completion];
}else{
//ios 9.0 之后是第二次返回不可用结果,此时采用的是指纹+密码策略,此时指纹确实不可用
//ios 9.0 之前,第一次返回指纹不可用的结果,此时直接返回指纹不可用的错误
_touchIDSecondCheck = NO;
completion(TIVerificationStatusNotEncrolled,[self returnErrorInfoWithErrorCode:code]);
} break;
}
}
if (@available(iOS 11.0, *)) {
if (code==LAErrorBiometryNotEnrolled) {
completion(TIVerificationStatusNotEncrolled,[self returnErrorInfoWithErrorCode:code]);
break;
}
} completion(TIVerificationStatusError,[self returnErrorInfoWithErrorCode:code]);
}
break; }
} #pragma mark - 生成error
+ (NSError *)returnErrorInfoWithErrorCode:(int)errorCode { if (TI_IS_iOS9_LATER) {
if (errorCode == LAErrorAppCancel) {
NSError *tempError = [NSError errorWithDomain:@"TouchIDVerfityDomain" code:TITouchIDVerificationErrorAppCancel userInfo:@{@"message":TouchIDCancelMessage}];
return tempError;
}
} NSString *errorMessage = @"";
NSUInteger errCode = ; switch (errorCode) {
case LAErrorTouchIDNotAvailable:
errorMessage = TouchIDNotAvailableMessage;
errCode = TITouchIDVerificationErrorNotAvailableMessage;
break;
case LAErrorAuthenticationFailed:
errorMessage = TouchIDNotCorrectMessage;
errCode = TITouchIDVerificationErrorAuthFailMessage;
break;
case LAErrorPasscodeNotSet:
errorMessage = TouchIDPasscodeNotSetMessage;
errCode = TITouchIDVerificationErrorPasscodeNotSetMessage;
break;
case LAErrorUserCancel:
errorMessage = TouchIDCancelMessage;
errCode = TITouchIDVerificationErrorUserCancel;
break;
default:
if (TI_IS_iOS9_LATER) {
if (errorCode==LAErrorTouchIDLockout) {
errorMessage = TouchIDLockOutMessage;
errCode = TITouchIDVerificationErrorLockOut;
break;
}
}
if (TI_IS_iOS8_LATER&&!TI_IS_iOS11_LATER) {
if (errorCode==LAErrorTouchIDNotEnrolled) {
errorMessage = TouchIDNotEnrolledMessage;
errCode = TITouchIDVerificationErrorNotEnrolledMessage;
break;
}
}
if (@available(iOS 11.0, *)) {
if (errorCode==LAErrorBiometryNotEnrolled) {
errorMessage = TouchIDNotEnrolledMessage;
errCode = TITouchIDVerificationErrorNotEnrolledMessage;
break;
}
}
errorMessage = TouchIDUnknownErrorMessage;
errCode = TITouchIDVerificationErrorUnknown;
break;
} NSError *tempError = [NSError errorWithDomain:@"TouchIDVerfityDomain" code:errCode userInfo:@{@"message":errorMessage}];
//
return tempError;
}
@end
ios --指纹TouchID的更多相关文章
- iOS 指纹解锁 验证TouchID
iOS指纹解锁 1.首先,引入依赖框架 LocalAuthentication.framework #import <LocalAuthentication/LocalAuthenticatio ...
- iOS 指纹认证登陆开发(TouchID)
设计思路 TouchID 关联账号 用户登陆成功 -> 开启TouchID登陆 -> TouchID验证 -> 记录用户信息(验证通过) -> 关联完成 TouchID 登陆 ...
- IOS指纹识别调用
最近正在开发的一个app需要加入指纹识别的功能,先搜索一下找到官方文档,简单易懂: https://developer.apple.com/library/ios/documentation/Loca ...
- iOS指纹识别Touch ID的安全性探讨
苹果公司在 iPhone 5s 的发布会上公布了全新的指纹识别安全技术,也就是 Touch ID,开创了生物安全识别技术在便携设备上使用的新篇章.此后,苹果还将此技术带到了 iPad 上.此前没有任何 ...
- iOS指纹识别代码
1:添加LocalAuthentication.framework框架 2:实现过程 #import "ViewController.h" #import <LocalAut ...
- iOS 指纹解锁
目前常用的App支持指纹解锁的还不是很多,如果在你的项目中用一下是不是显得高大上呢? 废话不说多,干货- 1.在工程中添加LocalAuthentication.framework 2.在需要验证的c ...
- iOS指纹识别
#import "ViewController.h" #import <LocalAuthentication/LocalAuthentication.h> @inte ...
- IOS 指纹识别的简单使用
首先导入LocalAuthentication框架 然后导入头文件 #import <LocalAuthentication/LAPublicDefines.h> - (void)begi ...
- ios 指纹识别解锁
:添加LocalAuthentication.framework框架 :实现过程 #import "ViewController.h" #import <LocalAuthe ...
随机推荐
- ota升级动画修改
在网上可以搜到很多相关的文章,但是很多文章都是复制粘贴而来的,为了方便后面工作学习,本文会把其中最关键的几个步骤列出来. 首先根据ota升级界面的文字可以确认相关的图片资源的目录在哪里,可以网上搜一下 ...
- JDK、JRE、JVM之间的关系
JDK.JRE.JVM之间的关系 1.JDK下载地址 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads ...
- MFC 多窗口通信时,使用RadioButton和Button时冲突问题
最近项目需要我们实现在两个窗口间进行通信,其中有个小功能如图所示: 当我点击GDIProgram中的Button1时,会更新Dialog的Radio1和Radio2的状态. Dialog中的Radio ...
- MongoDB 数据库
数据库: 关系型数据库 mysql 收费 速度快 字段类型 非关系型数据库 MongoDB 不收费 速度慢一些 存储数据都是字符串 ...
- JavaScript实现页面显示倒计时功能
下面是用JS中的日期函数和定时器完成的一个倒计时的例子,效果如图: 代码如下: <!DOCTYPE html> <html> <head> <title> ...
- 第31节:Java基础-类与对象
前言 Java基础-类与对象,方法的重载,构造方法的重载,static关键字,main()方法,this关键字,包,访问权限,类的继承,继承性,方法的重写,super变量. 方法的重载:成员方法的重载 ...
- java相关技术问答(一)
网上一些没有标准答案的面试题,我自己做的解答总结,有任何异议可以提出来~^_^,不断更新中... Springboot除了自动配置与传统的spring还有哪些不同 传统的springweb项目需要部署 ...
- Error: [ng:areq] Argument ‘AppCtrl’ is not a function, got undefined
今天把用ionic做一个案例,和ionic示例项目差不多,只是用requirejs分离了controller,但是一直报错 Error: [ng:areq] Argument ‘AppCtrl’ is ...
- linux下应用程序性能剖分神器gprofiler-tools-安装和使用
最近在摆弄算法的的优化,需要剖分一下算法的瓶颈,就找了一些代码剖分工具,其中 gprofileer-tools是很不错的工具,gperftools时google开源的一款C++性能分析分析工具,git ...
- Info - 信息分析思路概要
信息分析要素 局部 --->整体 显性 --->隐性 表面 --->本质 割裂 --->联系 特殊 --->普遍 串行 --->并发 纵向 --->横向 单点 ...