首先要进行判断,版本号支持最低8.0,如果低于8.0,账号密码形式。
/**
指纹识别本质上只是判断手机当前的主人,一旦判断成功,可以简化流程,但是实际操作不能少!
*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//[self inputUserInfo];
// 0. 判断设备的版本
NSLog(@"%@", [UIDevice currentDevice].systemVersion);
if ([UIDevice currentDevice].systemVersion.floatValue < 8.0) {
[self inputUserInfo];
return;
}
// 1. 判断设备是否支持指纹识别
LAContext *ctx = [[LAContext alloc] init];
//Biometrics单词 生物识别
if ([ctx canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:NULL]) {
NSLog(@"请按手指");
// 输入指纹
[ctx evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"大减价" reply:^(BOOL success, NSError *error) {
NSLog(@"%d %@ %@", success, error, [NSThread currentThread]);
// 1. 如果输入成功,直接购买
if (success) {
[self purchase];
} else {
// 判断错误类型是否是自行输入密码
if (error.code == LAErrorUserFallback) {
dispatch_async(dispatch_get_main_queue(), ^{
[self inputUserInfo];
});
}
}
}];
} else {
NSLog(@"不支持指纹");
[self inputUserInfo];
}
}
- (void)inputUserInfo {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"tip" message:@"请输入用户名&密码" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
// 指定 alert 的样式
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
// 显示 alertView
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"%zd", buttonIndex);
if (buttonIndex == 0) {
return;
}
// 获取 alert 中的内容
UITextField *username = [alertView textFieldAtIndex:0];
UITextField *pwd = [alertView textFieldAtIndex:1];
// 比较内容
if ([username.text isEqualToString:@"zhang"] && [pwd.text isEqualToString:@"123"]) {
[self purchase];
} else {
[[[UIAlertView alloc] initWithTitle:@"提示" message:@"用户名或者密码错误" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil] show];
}
}
//关于指纹识别 中各种情况枚举类型 详细分析
- (IBAction)showTouchIDAlert:(id)sender {
// 1.判断是否是iOS8之后
if (!iOS8later) {
NSLog(@"版本不对不能使用TouchID");
return;
}
// 2.调用touchID的相关方法
[self authenticateUser];
}
// 鉴定用户
- (void)authenticateUser
{
// 创建指纹验证对象
LAContext *context = [[LAContext alloc] init];
NSError *yfError = nil;
// 验证设备是否支持touchID
// LAPolicyDeviceOwnerAuthenticationWithBiometrics 14年时候枚举只有这一个属性
// LAPolicyDeviceOwnerAuthentication 后来加的枚举属性
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&yfError]) {
// 支持touchID
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:@"XMGlocalizedReason"
reply:^(BOOL success, NSError * _Nullable error) {
if (success) {
// touchID验证成功
// 继续处理相关业务(注意线程)
}else
{
NSLog(@"%@",error.localizedDescription);
switch (error.code) {
case LAErrorSystemCancel:
{
NSLog(@"Authentication was cancelled by the system");
//切换到其他APP,系统取消验证Touch ID
break;
}
case LAErrorUserCancel:
{
NSLog(@"Authentication was cancelled by the user");
//用户取消验证Touch ID
break;
}
case LAErrorUserFallback:
{
NSLog(@"User selected to enter custom password");
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//用户选择输入密码,切换主线程处理
}];
break;
}
default:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//其他情况,切换主线程处理
}];
break;
}
}
}
}];
}else
{
switch (yfError.code) {
case LAErrorTouchIDNotEnrolled:
NSLog(@"LAErrorTouchIDNotEnrolled");
break;
case LAErrorPasscodeNotSet:
NSLog(@"LAErrorPasscodeNotSet"); // 此处触发showPasscodeResetAlert方法
break;
default:
NSLog(@"Touch ID is unaviliable");
break;
}
NSLog(@"%@", yfError.localizedDescription);
}
}
/*
typedef NS_ENUM(NSInteger, LAError)
{
//授权失败
LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,
//用户取消Touch ID授权
LAErrorUserCancel = kLAErrorUserCancel,
//用户选择输入密码
LAErrorUserFallback = kLAErrorUserFallback,
//系统取消授权(例如其他APP切入)
LAErrorSystemCancel = kLAErrorSystemCancel,
//系统未设置密码
LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet,
//设备Touch ID不可用,例如未打开
LAErrorTouchIDNotAvailable = kLAErrorTouchIDNotAvailable,
//设备Touch ID不可用,用户未录入
LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled,
} NS_ENUM_AVAILABLE(10_10, 8_0);
*/