热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ios指纹TouchID

最近在做一套登录系统,包括指纹登录、手势登录以及账号密码登录,在此简单记录一下指纹的处理逻辑。指纹处理:1、处理iOS9.0之后ÿ

  

  最近在做一套登录系统,包括指纹登录、手势登录以及账号密码登录,在此简单记录一下指纹的处理逻辑。

  


 

 

指纹处理:
1、处理iOS 9.0之后,指纹被锁不会自动弹出密码解锁的情况;
2、处理iOS 10.*系统不区分未录入指纹和未设置密码的情况;
流程图:此处有图,周末再画
周末补上
代码如下:
.h:

// Created by ever on 17/3/7.
// Copyright © 2017年 ever. All rights reserved.
//
#import
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

 

.m:

// Created by ever on 17/3/7.
// Copyright © 2017年 ever. All rights reserved.#import "LYYTouchID.h"
#import
#import #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:&notSupportError]) {//处理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.*系统不走passwordNotSetif (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 = 0;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

 


转:https://www.cnblogs.com/sunjianfei/p/7885927.html



推荐阅读
  • 这个报错出现在userDao里面,sessionfactory没有注入。解决办法:spring整合Hibernate使用test测试时要把spring.xml和spring-hib ... [详细]
  • 拖拉切割直线 ... [详细]
  • 本文讨论了在使用表单上传文件时遇到的值为空问题,并提供了几种有效的解决方案。 ... [详细]
  • iOS 面试实战:15 道经典面试题及解析
    本文精选了15道iOS面试题,并提供了详细的解答思路。旨在帮助开发者更好地准备面试,避免因准备不足而导致的紧张和焦虑。 ... [详细]
  • 本文提供了详细的指导,帮助开发者了解如何使用PHP插件进行网站内容的翻译,特别是针对WordPress插件和主题的汉化及多语言支持。 ... [详细]
  • 本文档详细介绍了如何在Android应用中实现侧滑菜单(SlidingMenu)功能,包括设置侧边栏、全屏触摸模式以及初始化Fragment的具体步骤。 ... [详细]
  • 本文介绍了如何在Java中使用`JCheckBoxMenuItem.setMnemonic()`方法,并提供了多个实际应用的代码示例。 ... [详细]
  • 探讨如何在C++中,当子类实例存储在父类类型的向量中时,正确访问子类特有的成员变量或方法。 ... [详细]
  • C#爬虫Fiddler插件开发自动生成代码
    哈喽^_^一般我们在编写网页爬虫的时候经常会使用到Fiddler这个工具来分析http包,而且通常并不是分析一个包就够了的,所以为了把更多的时间放在分析http包上,自动化生成 ... [详细]
  • 在现代移动应用开发中,尤其是iOS应用,处理来自服务器的JSON数据是一项基本技能。无论是使用Swift还是PHP,有效地解析和利用JSON数据对于提升用户体验至关重要。本文将探讨如何在Swift中优雅地处理JSON,以及PHP中处理JSON的一些技巧。 ... [详细]
  • JobScheduler5.0源码分析
    0.JobScheduler执行代码mJobScheduler(JobScheduler)getSystemService(Context.JOB_SCHEDULER_SERVICE); ... [详细]
  • 如何清空Layui树结构
    本文将详细介绍如何使用Layui框架清空树形结构,包括创建树、添加节点以及实现清空功能的具体步骤。通过本文,您将能够掌握Layui树的管理技巧。 ... [详细]
  • 本文详细介绍了如何使用 HTML 和 CSS 实现一个具有动画效果的手风琴组件,包括代码示例和实现原理。 ... [详细]
  • 本文详细介绍了在Mac操作系统中使用Python连接MySQL数据库的方法,包括常见的错误处理及解决方案。 ... [详细]
  • 为什么会崩溃? ... [详细]
author-avatar
mobiledu2502907423
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有