iOS指紋識別比較簡單,主要由LAContontext類中的兩種方法完成:
1. - (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * autoreleasing *)error __attribute((swift_error(none))) 檢測設備是否支持指紋識別;
2. - (void)evaluatePolicy:(LAPolicy)policy
localizedReason:(NSString *)localizedReason
reply:(void(^)(BOOL success, NSError * __nullable error))reply 識別指紋
代碼:
LAContext *context = [LAContext new];
context.localizedCancelTitle = @"取消";
context.localizedFallbackTitle = @"密碼登錄";
NSError *error = nil;
BOOL supportFingerPrint = [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];
if (!supportFingerPrint) {
NSLog(@"不支持指紋解鎖:%@",error.localizedDescription);
switch (error.code) {
case LAErrorTouchIDNotEnrolled:
NSLog(@"TouchID Not Enrolled");
break;
case LAErrorPasscodeNotSet:
NSLog(@"Password Not Set");
break;
default:
NSLog(@"TouchID Not Available");
break;
}
} else {
NSLog(@"支持指紋解鎖");
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"指紋解鎖" reply:^(BOOL success, NSError * _Nullable error) {
if(success) {
NSLog(@"指紋解鎖成功");
} else {
switch (error.code) {
case LAErrorAuthenticationFailed: {
NSLog(@"Authentication Failed");
}
break;
case LAErrorUserCancel: {
NSLog(@"User Canceled");
}
break;
case LAErrorUserFallback: {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"User Fallback");
}];
}
break;
case LAErrorSystemCancel: {
NSLog(@"System Cancel");
}
break;
case LAErrorPasscodeNotSet: {
NSLog(@"Password Not Set");
}
break;
case LAErrorTouchIDNotAvailable: {
NSLog(@"TouchID Not Available");
}
break;
case LAErrorTouchIDNotEnrolled: {
NSLog(@"TouchID Not Enrolled");
}
default: {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"Other Targat ");
}];
}
break;
}
}
}];
}