您的位置:首页 > 移动开发 > IOS开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: