iOS TouchID 指纹识别
2016-04-01 11:16
197 查看
//
// ViewController.m
// touchID
//
// Created by 谢泽锋 on 16/4/1.
// Copyright © 2016年 xiezefeng. All rights reserved.
//
#import "ViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>
@interface ViewController ()
//验证设备是否支持Touch ID
- (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error;
//进行验证的回调
- (void)evaluatePolicy:(LAPolicy)policy
localizedReason:(NSString *)localizedReason
reply:(void(^)(BOOL success, NSError *error))reply;
//取消Touch Id验证的按钮的标题,默认标题是输入密码
@property (nonatomic, copy) NSString *localizedFallbackTitle;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//新建LAContext实例
LAContext *authenticationContext= [[LAContext alloc]init];
NSError *error;
//1:检查Touch ID 是否可用
if ([authenticationContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
NSLog(@"touchId 可用");
//2:执行认证策略
[authenticationContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"需要验证您的指纹来确认您的身份信息" reply:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(@"通过了Touch Id指纹验证");
}else{
NSLog(@"error===%@",error);
NSLog(@"code====%ld",error.code);
NSLog(@"errorStr ======%@",[error.userInfo objectForKey:NSLocalizedDescriptionKey]);
if (error.code == -2) {//点击了取消按钮
NSLog(@"点击了取消按钮");
}else if (error.code == -3){//点输入密码按钮
NSLog(@"点输入密码按钮");
}else if (error.code == -1){//连续三次指纹识别错误
NSLog(@"连续三次指纹识别错误");
}else if (error.code == -4){//按下电源键
NSLog(@"按下电源键");
}else if (error.code == -8){//Touch ID功能被锁定,下一次需要输入系统密码
NSLog(@"Touch ID功能被锁定,下一次需要输入系统密码");
}
NSLog(@"未通过Touch Id指纹验证");
}
}];
}else{
//todo goto 输入密码页面
NSLog(@"error====%@",error);
NSLog(@"抱歉,touchId 不可用");
}
// LAContext *laContext = [[LAContext alloc] init];
// NSError *error;
//
// if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
//
// [laContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
// localizedReason:@"你允许锋哥指纹识别吗"
// reply:^(BOOL success, NSError *error) {
// if (success) {
// NSLog(@"success to evaluate");
// //do your work
// }
// if (error) {
// NSLog(@"---failed to evaluate---error: %@---", error.description);
// //do your error handle
// }
// }];
// }
// else {
// NSLog(@"==========Not support :%@", error.description);
// //do your error handle
// }
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
// ViewController.m
// touchID
//
// Created by 谢泽锋 on 16/4/1.
// Copyright © 2016年 xiezefeng. All rights reserved.
//
#import "ViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>
@interface ViewController ()
//验证设备是否支持Touch ID
- (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error;
//进行验证的回调
- (void)evaluatePolicy:(LAPolicy)policy
localizedReason:(NSString *)localizedReason
reply:(void(^)(BOOL success, NSError *error))reply;
//取消Touch Id验证的按钮的标题,默认标题是输入密码
@property (nonatomic, copy) NSString *localizedFallbackTitle;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//新建LAContext实例
LAContext *authenticationContext= [[LAContext alloc]init];
NSError *error;
//1:检查Touch ID 是否可用
if ([authenticationContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
NSLog(@"touchId 可用");
//2:执行认证策略
[authenticationContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"需要验证您的指纹来确认您的身份信息" reply:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(@"通过了Touch Id指纹验证");
}else{
NSLog(@"error===%@",error);
NSLog(@"code====%ld",error.code);
NSLog(@"errorStr ======%@",[error.userInfo objectForKey:NSLocalizedDescriptionKey]);
if (error.code == -2) {//点击了取消按钮
NSLog(@"点击了取消按钮");
}else if (error.code == -3){//点输入密码按钮
NSLog(@"点输入密码按钮");
}else if (error.code == -1){//连续三次指纹识别错误
NSLog(@"连续三次指纹识别错误");
}else if (error.code == -4){//按下电源键
NSLog(@"按下电源键");
}else if (error.code == -8){//Touch ID功能被锁定,下一次需要输入系统密码
NSLog(@"Touch ID功能被锁定,下一次需要输入系统密码");
}
NSLog(@"未通过Touch Id指纹验证");
}
}];
}else{
//todo goto 输入密码页面
NSLog(@"error====%@",error);
NSLog(@"抱歉,touchId 不可用");
}
// LAContext *laContext = [[LAContext alloc] init];
// NSError *error;
//
// if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
//
// [laContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
// localizedReason:@"你允许锋哥指纹识别吗"
// reply:^(BOOL success, NSError *error) {
// if (success) {
// NSLog(@"success to evaluate");
// //do your work
// }
// if (error) {
// NSLog(@"---failed to evaluate---error: %@---", error.description);
// //do your error handle
// }
// }];
// }
// else {
// NSLog(@"==========Not support :%@", error.description);
// //do your error handle
// }
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- iOS多线程总结
- iOS学习笔记11--Xcode创建一个纯代码的工程
- iOS获取通讯录问题
- iOS _隐藏Navigation 返回按钮
- iOS开发系列之Xcode插件--Alcatraz的安装和使用
- iOS开发中手机号码和价格金额有效性判断及特殊字符的限制
- iOS开发--轮播图
- iOS开发之--- NSURLProtocol
- iOS锁屏问题解决方式
- Routable-iOS
- iOS 开发者总结
- Xcode7找不到.dylib解决方法
- 在ios上时间无法parse返回 "Invalid Date"
- iOS 面向对象
- iOS 基本数据类型与函数
- iOS 开发之动画篇 - Transform和KeyFrame动画
- iOS FMDB使用与缓存数据
- ios开发--集成银联3.3.0
- iOS截图整个屏幕
- iOS开发---给数字添加删除线