指纹识别身份

本文介绍了一种在iOS应用中集成TouchID的方法。通过引入QuartzCore.framework和LocalAuthentication.framework框架,并利用Objective-C代码实现了一个简单的示例,展示了如何使用TouchID进行用户身份验证。

最近因为要做一个理财产品,所以指纹识希望加进去,就研究了下,写了段代码

先在项目中添加QuartzCore.framework和LocalAuthentication.framework

 

#import "ViewController.h"

#import <LocalAuthentication/LocalAuthentication.h>

 

 

@interface ViewController ()

 

@property (weak, nonatomic) IBOutlet UIButton *btn;

@end

 

@implementation ViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

 

    _btn.layer.shadowColor = [UIColor grayColor].CGColor;

    _btn.layer.shadowOffset = CGSizeMake(3, 2);

    _btn.layer.shadowOpacity = 0.5;

    _btn.center = self.view.center;

 

    [_btn setTitle:@"Touch IDTest" forState:UIControlStateNormal];

    [_btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    

    // Do any additional setup after loading the view, typically from a nib.

}

 

-(void)btnClick:(UIButton *)sender

{

    [self TouchIDTestSuccess:^

    {

        NSLog(@"       Touch ID     ");

        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示"  message:@"恭喜,您通过了Touch ID身份验证" preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction * cancel= [UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleCancel handler:nil];

        [alert addAction:cancel];

        [self presentViewController:alert animated:YES completion:nil];

    } Failure:^(NSError *error)

    {

        

        NSLog(@"        Touch ID     \n\(error)");

        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示"  message:@"抱歉,您未能通过Touch ID身份验证"  preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction * cancel= [UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleCancel handler:nil];

        [alert addAction:cancel];

        [self presentViewController:alert animated:YES completion:nil];

        

    } Unvilabe:^(NSError *error)

     {

        NSLog(@"        Touch ID     \n\(error)");

        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示"  message:@"抱歉,Touch ID不可用"  preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction * cancel= [UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleCancel handler:nil];

        [alert addAction:cancel];

        [self presentViewController:alert animated:YES completion:nil];

    }];

 

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

-(void)TouchIDTestSuccess:(void(^)())successed Failure:(void(^)(NSError * error))failured  Unvilabe:(void(^)(NSError * error))Unvilabe

{

    LAContext * lauth = [[LAContext alloc]init];

    NSError * error  = nil;

    BOOL isTouchAvalible = [lauth canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics  error:&error];

    if (isTouchAvalible)

    {

        [lauth evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"需要验证您的指纹来确认您的身份信息" reply:^(BOOL success, NSError * _Nullable error)

         {

             if (success)

             {

                 if (successed)

                 {

                     successed();

                 }

             }

             else

             {

                 if (failured)

                 {

                     failured(error);

                 }

             }

         }];

    } else

    {

        if (Unvilabe)

        {

            Unvilabe(error);

        }

    }

}

 

转载于:https://www.cnblogs.com/Hakim/p/5337992.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值