码迷,mamicode.com
首页 > 移动开发 > 详细

iOS TouchID 指纹识别

时间:2016-04-01 12:46:18      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

//
//  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 TouchID 指纹识别

标签:

原文地址:http://www.cnblogs.com/xiezefeng/p/5344272.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!