iOS 8 Touch ID错误“需要用户交互。”

我一直在努力将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]; }); } }); }]; } } 

通常PIN视图控制器在进入后台之前被推入:

 - (void)applicationDidEnterBackground:(UIApplication *)application 

所以当通过应用程序预览图像分页时,应用程序的内部信息将不会显示(双击主页button)。 我猜你正在做类似的事情。

问题是LocalAuthentication的新API需要调用viewController是可见的。 这就是为什么你不应该在退出后台之前调用你的“showTouchID”函数。 进入前景时请改用“showTouchID”function:

 - (void)applicationWillEnterForeground:(UIApplication *)application 

它应该工作。 不要忘记在应用程序首次启动时调用它(在这种情况下,将不会调用.willEnterForeground)。

@hetzi答案真的帮了我,但我有更多的补充。

基本上这个错误发生在你的应用从后台被唤醒的时候,以及你要求的Touch ID(我的情况是本地authenticationtypes,我没有用钥匙串typestesting)。 当应用程序在后台运行时,用户无法与Touch ID进行交互,因此出现错误消息。

用户交互是必需的。

我的应用程序来自后台的原因是: 推送通知Apple Watch

我的修补程序在我的初始VC的viewDidLoad方法上做了这样的事情:

 if ([UIApplication sharedApplication].applicationState != UIApplicationStateBackground) { [self promptForTouchID]; } 

我已经使用!=因为,当你的应用程序第一次启动它是在UIApplicationStateInactive状态。 并且该状态不会生成Touch ID错误,因为会出现提示。

我也在UIApplicationWillEnterForegroundNotification的通知上调用[self promptForTouchID] ,但由于您知道应用程序将进入前台,因此无需在此处查看。