我一直致力于将 Touch ID 支持集成到我正在开发的应用程序中。然而,它的行为非常不一致。我看到的一个常见问题是在新的应用程序启动时它按预期工作,但是在后台应用程序并将其带到前台时,我收到了一个错误
evaluatePolicy:localizedReason:reply:
它甚至没有多大意义(我从来没有看到过 touchid 警报)
Error Domain=com.apple.LocalAuthentication Code=-1004 "User interaction is required." UserInfo=0x171470a00 {NSLocalizedDescription=User interaction is required.}
我已经尝试在应用程序已经运行时显示 touchid 警报,当它刚刚出现时,似乎并不重要。它在初始应用程序启动后每次都会中断。
还有其他人遇到这个吗?
作为参考,这是我正在使用的代码:
if (_useTouchId && [LAContext class]) {
LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
_didPresentTouchId = YES;
[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"Use your Touch ID to open *****" reply:^(BOOL success, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^ {
if (success) {
_isClosing = YES;
[self hide];
if (_successBlock) {
_successBlock();
}
}
else if (error && error.code != -2 && error.code != -3 && error.code != -1004) {
[[[UIAlertView alloc] initWithTitle:@"Error" message:@"Authentication failed, please enter your Pin" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil] show];
}
else {
if (error) {
DDLogError(@"TouchID error: %@", error.description);
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, .6 * NSEC_PER_SEC), dispatch_get_main_queue(), ^ {
[self keyboardButtonTouched];
});
}
});
}];
}
}