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 ...
随机推荐
- Beta冲刺 (6/7)
Part.1 开篇 队名:彳艮彳亍团队 组长博客:戳我进入 作业博客:班级博客本次作业的链接 Part.2 成员汇报 组员1:(组长)柯奇豪 过去两天完成了哪些任务 部分代码的整合 编辑及标注的提交操 ...
- windows 批处理语言学习
程序员应该根植于心的一个理念是:重复的工作交给代码.windows上的批处理脚本就是这种理念的体现. 批处理bat能做的事很多,自动配置vs工程中的代码依赖环境,调用其它程序处理数据.自动编译代码等等 ...
- NET Core微服务之路:自己动手实现Rpc服务框架,基于DotEasy.Rpc服务框架的介绍和集成
本篇内容属于非实用性(拿来即用)介绍,如对框架设计没兴趣的朋友,请略过. 快一个月没有写博文了,最近忙着两件事; 一:阅读刘墉先生的<说话的魅力>,以一种微妙的,你我大家都会经常遇见 ...
- Windows核心编程:第10章 同步设备IO与异步设备IO
Github https://github.com/gongluck/Windows-Core-Program.git //第10章 同步设备IO与异步设备IO.cpp: 定义应用程序的入口点. // ...
- Java基础--枚举
1.枚举简介 枚举是由一组固定的常量组成的类型,自定义数据类型. 枚举的常量值一定是可列举的有限值.常量值的类型都是public static final. 下面代码中的Gender 是一种自定义的数 ...
- YUM仓库安装LAMP与LNMP
LAMP动态网站部署架构是由一套 Linux+Apache+MySQL+PHP 组成的动态网站系统解决方案. LNMP动态网站部署架构是由一套 Linux+Nginx+MySQL+PHP 组成的动态网 ...
- UIAlertControllerStyleActionSheet 崩溃。
即使Devices 设置为iPhone模式,在审核时还是运行在iPad的小屏模式下.因此必须 UIActivityViewController UIAlertControllerStyleAction ...
- vsftpd安装配置以及常见问题解决
vsftpd安装配置以及踩坑解决办法,Centos7 nginx已经配置成功了,但是使用http始终没办法访问到图片,那么你来对地方了(在文章末尾是原因) 配置nginx教程:http://blog. ...
- 机器学习与Tensorflow(5)——循环神经网络、长短时记忆网络
1.循环神经网络的标准模型 前馈神经网络能够用来建立数据之间的映射关系,但是不能用来分析过去信号的时间依赖关系,而且要求输入样本的长度固定 循环神经网络是一种在前馈神经网络中增加了分亏链接的神经网络, ...
- vueAdmin ui基础包
这个版本的vueAdmin ui是针对官网版本进行修改而成,主要修改了后端接口修改,登录等等,一个通用的前端工程基础包,即开即用,省去了前期繁琐配置 Demo Build Setup # Clone ...