iOS 简单的指纹识别

来源:互联网 发布:骑士vs火箭数据 编辑:程序博客网 时间:2024/05/16 19:50

#import "ViewController.h"

#import <LocalAuthentication/LocalAuthentication.h>


@interface ViewController ()


@property (strong,nonatomic) LAContext *context;


@end


@implementation ViewController


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    // 弹出指纹验证

    // 注意:在使用过程当中context对象不能被释放

    self.context = [[LAContext alloc] init];

    

    // 验证设备是否支持Touch ID (5S后支持)

    /**

     LAPolicyDeviceOwnerAuthentication    TouchID或密码   iOS9

     LAPolicyDeviceOwnerAuthenticationWithBiometrics    生物信息识别(Touch ID)   ~iOS8

     */

    NSError *error = nil;

    BOOL evalute = [self.context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];

    if (evalute == NO) {

        NSLog(@"不支持Touch ID");

        return;

    }

    

    // 向用户声明使用Touch ID验证目的

    NSString *reason = @"向我汇款1000";

    [self.context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:reason reply:^(BOOL success, NSError *_Nullable error) {

        

        if (success) {

            NSLog(@"指纹授权成功");

            

            // 汇款

        } else {

            // LAError 定义了错误的信息

            switch (error.code) {

                case LAErrorTouchIDLockout:

                    NSLog(@"Touch ID 已经上锁");

                    break;

                default:

                    break;

            }

        }

    }];

}


@end


0 0