我正在使用以下代码来检测设备中的Touch ID可用性,它可以正常工作
- (BOOL)canAuthenticateByTouchId
{
if ([LAContext class])
{
return [[[LAContext alloc] init] canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil];
}
return NO;
}
- (IBAction)touchIDAvailability
{
if([self canAuthenticateByTouchId])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Congrats"
message:@"Your device have TouchID"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
}
else
{
str = @"TouchID is not available in your device";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops"
message:str
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
}
}
检测到Touch ID可用性后,我想找到Touch ID是否已配置
NSString *str;
if (LAErrorTouchIDNotEnrolled)
{
str = @"Please configure your TouchID in Settings > Touch ID & Passcode";
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops"
message:str
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
我配置了Touch ID,但它仍然输入LAErrorTouchIDNotEnrolled这个循环。任何人都知道告诉我。
答案 0 :(得分:2)
您可以使用下面的
检查是否配置了用户[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:myLocalizedReasonString
reply:^(BOOL succes, NSError *error) {
}
以下是Touch ID的完整代码
LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
myContext.localizedFallbackTitle=@"Login with Password";//optional for hiding enter password button
NSString *myLocalizedReasonString = @"For touch ID Users Ignore this pop-up and proceed to login with you finger now";
if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:myLocalizedReasonString
reply:^(BOOL succes, NSError *error) {
if (succes) {
[self showMessage:@"Authentication is successful" withTitle:@"Success"];
NSLog(@"User authenticated"); }
} else {
switch (error.code) {
case LAErrorAuthenticationFailed:
[self showMessage:@"Authentication is failed" withTitle:@"Error"];
NSLog(@"Authentication Failed");
break;
case LAErrorUserCancel:
[self showMessage:@"You clicked on Cancel" withTitle:@"Error"];
NSLog(@"User pressed Cancel button");
break;
case LAErrorUserFallback:
[self showMessage:@"You clicked on \"Enter Password\"" withTitle:@"Error"];
NSLog(@"User pressed \"Enter Password\"");
break;
default:
[self showMessage:@"Touch ID is not configured" withTitle:@"Error"];
NSLog(@"Touch ID is not configured");
break;
}
NSLog(@"Authentication Fails");
}
}];
} else {
NSLog(@"Can not evaluate Touch ID");
NSString *str = @"TouchID is not available in your device";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops"
message:str
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
}
}
-(void) showMessage:(NSString*)message withTitle:(NSString *)title
{
[[[UIAlertView alloc] initWithTitle:@"AppName" message:message delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil] show];
}
希望它可以帮助你......!