iOS TouchID 指纹识别

Posted 谢小锋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS TouchID 指纹识别相关的知识,希望对你有一定的参考价值。

//
//  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 指纹识别的主要内容,如果未能解决你的问题,请参考以下文章

iOS8使用TouchID

iOS 关于TouchID指纹解锁的实现

iOS 之(个人隐私钱包调用系统TouchID指纹锁验证)

iOS指纹解锁手势解锁--Swift代码

iOS XCUITest 模拟指纹

CoreBluetooth - TouchID应用