指纹识别是在iOS8.0以后才推出的,所以我们如果想把指纹集成到我们的APP当中,我们首先就要在代码中判断iOS版本。接下来通过本文给大家分享iOS开发中指纹识别简单介绍,需要的朋友参考下吧
ios开发中指纹识别简单介绍,在iphone系列中,是从5S以后开始有了指纹识别的功能,在ios8的时候开放的指纹验证的接口。所以我们在进行指纹识别应用的时候要去判断机型以及系统的版本。
代码如下
//1、判断系统版本是不是大于等于8.0如果大于等于的话就表示可以使用指纹识别
if([UIDevice currentDevice].systemVersion.floatValue>=8.0)
{
//判断是否可以使用指纹识别的功能,是在5S之后才可以进行使用的
//创建LA对象的上下文
LAContext * context = [[LAContext alloc]init];
//判断设备是否支持指纹识别
//Evaluate 表示评估的意思
//Policy表示的是策略
//用来检查当前设备是否可用touchID
if([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil])
{
//LAPolicyDeviceOwnerAuthentication如果我们三次指纹输入的都错了,就会弹出密码框,如果不进行密码输入。再次进来还可以有两次机会验证指纹如果都
错误还会继续弹出系统密码框让你输入 如果你没输入touch ID就会被锁定,而LAPolicyDeviceOwnerAuthenticationWithBiometrics不会弹出输入系统
的密码框,输入三次错误之后,默认不会做任何处理,我们还可以重新再点击指纹识别进行输入,但是如果还是输入错误两次之后touch id就会被锁定
//表示可以使用指纹识别技术
[context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:@"请验证指纹进行支付" reply:^
(BOOL success, NSError * _Nullable error) {
//里面是在子线程中执行的,所以要更新UI的话,肯定是需要回到主线程去执行的
//判断是否成功
if(success)
{
NSLog(@"%@",[NSThread currentThread]);
NSLog(@"验证成功");
}
else
{
NSLog(@"验证失败");
}
NSLog(@"%@",[NSThread currentThread]);
NSLog(@"%@",error);
if(error)
{
if(error.code==-2)
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController * vc = [UIAlertController alertControllerWithTitle:@"指纹验证取消" message:@""
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"---------");
}];
UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"hhhhhh");
}];
[vc addAction:action];
[vc addAction:action1];
[self presentViewController:vc animated:YES completion:nil];
});
}
else if(error.code==-1)
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController * vc = [UIAlertController alertControllerWithTitle:@"指纹已经输错3次" message:
@"你还有两次机会" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"---------");
}];
[vc addAction:action];
[self presentViewController:vc animated:YES completion:nil];
});
}
}
}];
}
}
else
{
NSLog(@"对不起,系统版本过低");
}
需要特别注意的其实就是LAPolicyDeviceOwnerAuthentication
和LAPolicyDeviceOwnerAuthenticationWithBiometrics
的区别,以及检测系统的版本通过[UIDevice currentDevice].systemVersion.floatValue
,判断设备是否可用Touch ID就是通过canEvaluatePolicy: error:
这个方法来进行判断。还有需要注意的是下面验证指纹识别是否成功的操作默认都是在子线程中进行的,所以我们如果要做UI的操作要回到主线程去执行。可用利用dispatch_async(dispatch_queue_t _Nonnull queue, ^{ })
这个函数来实现,里面传入主队列即可。还有就是我们也可以根据eror的code来进行一些判断,看用户具体是因为什么原因导致的错误,然后在作出相应的输出。
以上所述是小编给大家介绍的iOS开发中指纹识别简单介绍,希望对大家有所帮助