苹果在iOS8之后开启了指纹识别的功能,如果想让自己的App能够使用指纹识别功能,必须要满足一定的条件才行。开发文档
1. 必须是iPhone5s之后的设备
2. 系统版本必须在等于或大于iOS8
具体实现
使用指纹识别,必须要引入LocalAuthentication.framework。引入之后,需要
import <LocalAuthentication/LocalAuthentication.h>。
实现代码如下:
//第一步判断系统版本是否大于或等于iOS8
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
LAContext *context = [LAContext new];
//判断设备是否支持指纹识别
if (![context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
NSLog(@"对不起,指纹识别技术暂时不可用");
}
//如果支持指纹识别,这是会弹出指纹识别框,进行识别,并根据返回进行相应操作
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"开启了指纹识别,将打开隐藏功能" reply:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(@"指纹识别成功");
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"指纹识别成功" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alertView show];
});
}
if (error) {
if (error.code == -2) {
NSLog(@"用户取消了操作");
// 取消操作,回主线程更新UI,弹出提示框
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"用户取消了操作" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alertView show];
});
} else {
NSLog(@"错误: %@",error);
// 指纹识别出现错误,回主线程更新UI,弹出提示框
NSString *code = [NSString stringWithFormat:@"%ld",error.code];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:code delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alertView show];
}
}
}];
}
else {
NSLog(@"对不起,该手机不支持指纹识别");
}
这里仅说明了启动指纹识别功能的方法,如果要加入到第三方App中,还需要和相应的服务端配合使用,以完成指纹识别验证。
错误代码
错误 | 描述 |
---|---|
LAErrorAuthenticationFailed | 指纹无法识别 |
LAErrorUserCancel | 用户点击了取消 |
LAErrorUserFallback | 用户取消,点击了输入密码按钮 |
LAErrorSystemCancel | 系统取消,其它应用进入前台 |
LAErrorPasscodeNotSet | 验证无法启动,因为设备上没有设置密码 |
LAErrorTouchIDNotAvailable | 验证无法启动,因为设备上TouchID无效 |
LAErrorTouchIDNotEnrolled | 验证无法启动,因为没有录入指纹 |