指纹识别身份
最近因为要做一个理财产品,所以指纹识希望加进去,就研究了下,写了段代码
先在项目中添加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);
}
}
}