iOS 官方 Demo

https://developer.apple.com/library/ios/samplecode/KeychainTouchID/Introduction/Intro.html

https://segmentfault.com/a/1190000002516465

基础知识

支持系统和机型

iOS系统的指纹识别功能最低支持的机型为iPhone 5s,最低支持系统为iOS 8,虽然安装iOS 7系统的5s机型可以使用系统提供的指纹解锁功能,但由于API并未开放,所以理论上第三方软件不可使用。

依赖框架

LocalAuthentication.framework

#import <LocalAuthentication/LocalAuthentication.h>

注意事项

iOS 8以下版本适配时,务必进行API验证,避免调用相关API引起崩溃。

使用类

LAContext 指纹验证操作对象

代码

- (void)authenticateUser {//初始化上下文对象LAContext* context = [[LAContext alloc] init];//错误对象NSError* error = nil;NSString* result = @"Authentication is needed to access your notes.";//首先使用canEvaluatePolicy 判断设备支持状态if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {//支持指纹验证[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {if (success) {//验证成功,主线程处理UI}else{NSLog(@"%@",error.localizedDescription);switch (error.code) {case LAErrorSystemCancel:{NSLog(@"Authentication was cancelled by the system");//切换到其他APP,系统取消验证Touch IDbreak;}case LAErrorUserCancel:{NSLog(@"Authentication was cancelled by the user");//用户取消验证Touch IDbreak;}case LAErrorUserFallback:{NSLog(@"User selected to enter custom password");[[NSOperationQueue mainQueue] addOperationWithBlock:^{//用户选择输入密码,切换主线程处理}];break;}default:{[[NSOperationQueue mainQueue] addOperationWithBlock:^{//其他情况,切换主线程处理 }];break;}}}}];}else{//不支持指纹识别,LOG出错误详情switch (error.code) {case LAErrorTouchIDNotEnrolled:{NSLog(@"TouchID is not enrolled");break;}case LAErrorPasscodeNotSet:{NSLog(@"A passcode has not been set");break;}default:{NSLog(@"TouchID not available");break;}}NSLog(@"%@",error.localizedDescription);} }
typedef NS_ENUM(NSInteger, LAError) {//授权失败LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,//用户取消Touch ID授权LAErrorUserCancel = kLAErrorUserCancel,//用户选择输入密码LAErrorUserFallback = kLAErrorUserFallback,//系统取消授权(例如其他APP切入)LAErrorSystemCancel = kLAErrorSystemCancel,//系统未设置密码LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet,//设备Touch ID不可用,例如未打开LAErrorTouchIDNotAvailable = kLAErrorTouchIDNotAvailable,//设备Touch ID不可用,用户未录入LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled, } NS_ENUM_AVAILABLE(10_10, 8_0);

操作流程

首先判断系统版本,iOS 8及以上版本执行-(void)authenticateUser方法,方法自动判断设备是否支持和开启Touch ID

iOS 9

感谢秋儿指出iOS 9加入了三种新的错误类型。

/// Authentication was not successful, because there were too many failed Touch ID attempts and/// Touch ID is now locked. Passcode is required to unlock Touch ID, e.g. evaluating/// LAPolicyDeviceOwnerAuthenticationWithBiometrics will ask for passcode as a prerequisite.LAErrorTouchIDLockout NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorTouchIDLockout,/// Authentication was canceled by application (e.g. invalidate was called while/// authentication was in progress).LAErrorAppCancel NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorAppCancel,/// LAContext passed to this call has been previously invalidated.LAErrorInvalidContext NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorInvalidContext

其中,LAErrorTouchIDLockout是在8.0中也会出现的情况,但并未归为单独的错误类型,这个错误出现,源自用户多次连续使用Touch ID失败,Touch ID被锁,需要用户输入密码解锁,这个错误的交互LocalAuthentication.framework已经封装好了,不需要开发者关心。

LAErrorAppCancelLAErrorSystemCancel相似,都是当前软件被挂起取消了授权,但是前者是用户不能控制的挂起,例如突然来了电话,电话应用进入前台,APP被挂起。后者是用户自己切到了别的应用,例如按home键挂起。

LAErrorInvalidContext很好理解,就是授权过程中,LAContext对象被释放掉了,造成的授权失败。

//
//  ViewController.m
//  test_ touch_ID_01
//
//  Created by admin on 2/15/16.
//  Copyright © 2016 jeffasd. All rights reserved.
//#import "ViewController.h"#import <LocalAuthentication/LocalAuthentication.h>@interface ViewController ()@property(nonatomic, strong) UIButton *authority;
@property(nonatomic, strong) UIButton *entryButton;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];_authority = [UIButton buttonWithType:UIButtonTypeCustom];_authority.frame = CGRectMake(100, 100, 200, 50);_authority.backgroundColor = [UIColor cyanColor];[_authority setTitle:@"是否支持" forState:UIControlStateNormal];[_authority addTarget:self action:@selector(canEvaluatePolicy) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:_authority];}#pragma mark - evaluatePolicy
- (void)canEvaluatePolicy{LAContext *context = [LAContext new];__block NSString *message;NSError *error;BOOL success;success = [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];if (success) {message = [NSString stringWithFormat:@"Touch ID is available"];}else{message = [NSString stringWithFormat:@"Touch ID is not available"];NSLog(@"%@", message);switch (error.code) {case LAErrorTouchIDNotEnrolled:NSLog(@"TouchID is not enrolled");break;case LAErrorPasscodeNotSet:NSLog(@"A passcode has not been set");break;default:NSLog(@"TouchID not available");break;}NSLog(@"localized %@",error.localizedDescription);}NSLog(@"%@", message);
}- (IBAction)entryButton:(UIButton *)sender {LAContext *context = [LAContext new];__block NSString *message;// Set text for the localized fallback button.
//    context.localizedFallbackTitle = @"Enter Password 111";
//    context.localizedFallbackTitle = @"111";
//    context.localizedFallbackTitle = @"";//show the authentication UI with our reason string[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"Unlock access to locked feature" reply:^(BOOL success, NSError * _Nullable authenticationError) {if (success) {//验证成功,主线程处理UImessage = @"evaluatePolicy : success";}else{message = [NSString stringWithFormat:@"evaluatePolicy %@", authenticationError.localizedDescription];NSLog(@"%@", message);NSLog(@"%@",authenticationError.localizedDescription);switch (authenticationError.code) {case LAErrorSystemCancel:{NSLog(@"Authentication was cancelled by the system");//切换到其他APP,系统取消验证Touch IDbreak;}case LAErrorUserCancel:{NSLog(@"Authentication was cancelled by the user");//用户取消验证Touch IDbreak;}case LAErrorUserFallback:{NSLog(@"User selected to enter custom password");[[NSOperationQueue mainQueue] addOperationWithBlock:^{//用户选择输入密码,切换主线程处理UIView *view = [UIView new];view.frame = self.view.bounds;view.backgroundColor = [UIColor yellowColor];[self.view addSubview:view];}];break;}case LAErrorAuthenticationFailed:{NSLog(@"User kLAErrorAuthenticationFailed");[[NSOperationQueue mainQueue] addOperationWithBlock:^{//用户选择输入密码,切换主线程处理}];break;}default:{[[NSOperationQueue mainQueue] addOperationWithBlock:^{//其他情况,切换主线程处理}];break;}}}NSLog(@"%@", message);}];
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end

iOS touchID 处理办法相关推荐

  1. iOS Bug解决办法:如何防止Siri读出隐藏的通知

    文章来源:ATYUN AI平台 尽管苹果尚未针对iOS错误展开修复,使得Siri能够读出隐藏的锁屏通知,但以下方法可以解决目前的安全漏洞. 苹果的内部人员分享了一些解决办法,以防止Siri在锁定屏幕上 ...

  2. iOS TouchID和FaceID登录验证 简单使用

    新入职公司 还是接受了之前的项目 提出新的需求 用指纹和面部进行安全验证和登录 涉及到TouchID和FaceID相关技术知识点 查找资料后简单了解并实现功能 先来点直观的图 指纹识别和面部识别公用一 ...

  3. iOS TouchID/FaceID 开发

    创建LXFAuthentication类的.h和.m文件,继承自LAContext .h代码如下: // // LXFAuthentication.h // Test // // Created by ...

  4. 手机端问题IOS及解决办法

    1.解决页面使用 overflow: scroll 在 iOS 上滑动卡顿的问题? 首先你可能会给页面的 html 和 body 增加了 height: 100%, 然后就可能造成 IOS 上页面滑动 ...

  5. html5 ios iframe src,iframe不支持ios的解决办法

    iframe 直接在ios APP中运行,可能有问题,解决办法可以参考如下代码 案例一 #ifram { border: 0; width: 1px; min-width: 100%; *width: ...

  6. iOS TouchID验证和Keychain结合使用

    1.TouchID的简单实现 首先先导入LocalAuthentication/LocalAuthentication.h头文件 使用TouchID前先检测TouchID是否可用,然后再调用 LACo ...

  7. flash builder4.7 找不到IOS设备解决办法

    换了新电脑后,用flash builder 调试游戏,但是发现总是提示找不IOS设备,其实设备已经连接到电脑上了,而且能正常从电脑安装APP到手机. 是什么原因呢? 最终通过google,发现是itu ...

  8. iOS不能跳转到支付宝的解决办法

    iOS不能跳转到支付宝的解决办法 对接APP/H5支付时,可能会遇到在加载支付页面时,不能跳转到支付宝app(手机已经安装支付宝),目前iOS版本解决办法如下 以UIWebView为例 android ...

  9. Cognex Mobile Barcode SDK for iOS

    概述 Cognex Mobile Barcode SDK (cmbSDK) 是用于开发移动条码扫描应用程序的SDK. SDK是付费的,但功能很强大. Cognex Mobile Barcode SDK ...

最新文章

  1. [Dynamic Language] Python 静态方法、类方法、属性
  2. 【OF框架】使用OF.WinService项目,添加定时服务,进行创建启动停止删除服务操作...
  3. 富盛Sbo生产管理简介
  4. 十佳运动员有奖评选系统_2019年度国际足坛十佳运动员,利物浦三星在列,第十名属私心...
  5. 80%的人都不知道的排版利器,博士生都在用它!
  6. Invalid bound statement (not found)
  7. linux脚本登录启动失败,linux – 在X上运行shell脚本失败登录尝试
  8. 为Get/Post课程收集资料
  9. linux系统安装升级win10双系统,Win10 安装Linux ubuntu-18.04双系统(安装指南)
  10. android点击出现菜单,Android 点击按钮弹出菜单
  11. [项目回顾]基于Annotation与SpringAOP的缓存简单解决方案
  12. Android Drawable之getIntrinsicWidth()和getIntrinsicHeight()
  13. 菜鸟评python,F#,Go
  14. mongoDB地理位置查询
  15. Ubuntu16.04中修复Pycharm问号图标问题
  16. Linux:搭建GIT服务,Linux中使用git,git基础命令,和原理
  17. 个人小程序开发有哪些限制?
  18. Lambda 表达式详解
  19. Arcgis 熟练和操作
  20. 三-五功能/半亮/25%亮/全亮/爆闪/SOS_专用应急灯手电筒IC方案

热门文章

  1. 洛谷-P1428-小鱼比可爱
  2. cloudreve 开源私有网盘(带离线下载)
  3. 干货!Java基础知识梳理,绝对经典
  4. 窃隐私泄露、放高利贷,输入法的暗箱操作
  5. 上海航芯|物联网安全芯片ACL16简介
  6. origin常用函数
  7. stm32—酒精传感器的初步使用
  8. 项目 编码规则(编写代码规则)
  9. 从360推出无广告的极速版,谈到一般人对杀毒软件的无知…
  10. 极路由通过SSH添加静态路由表之后无法跳转的问题