身份验证失败,再次尝试面部识别无效。

6

我只是按照这个教程来通过面部识别进行用户认证,但实际上在认证失败后,"再试一次面部 ID" 按钮无法工作,回调函数没有被调用,我不知道为什么。 以下是代码:

@IBAction func touchIdAction(_ sender: UIButton) {

    print("hello there!.. You have clicked the touch ID")

    let myContext = LAContext()
    let myLocalizedReasonString = "Biometric Authntication testing !! "

    var authError: NSError?
    if #available(iOS 8.0, macOS 10.12.1, *) {
        if myContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
            myContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: myLocalizedReasonString) { success, evaluateError in

                DispatchQueue.main.async {
                    if success {
                        // User authenticated successfully, take appropriate action
                        self.successLabel.text = "Awesome!!... User authenticated successfully"
                    } else {
                        // User did not authenticate successfully, look at error and take appropriate action
                        self.successLabel.text = "Sorry!!... User did not authenticate successfully"
                    }
                }
            }
        } else {
            // Could not evaluate policy; look at authError and present an appropriate message to user
            successLabel.text = "Sorry!!.. Could not evaluate policy."
        }
    } else {
        // Fallback on earlier versions

        successLabel.text = "Ooops!!.. This feature is not supported."
    }


}

在 iPhone X 模拟器上使用 Xcode 9.4.1 运行。 谢谢。
2个回答

7
在点击“再次尝试面容ID”按钮之前,您需要通知模拟器它是否应该模拟匹配或不匹配的面容。
您可以通过选择“硬件-> 面容ID-> 匹配面容/不匹配面容”来实现。

enter image description here


@maphongba008 好的,我刚刚更新了答案。它应该解决你的问题。 - loup4
2
好的,我刚试了一下,但是很奇怪,我在点击“再次尝试FaceID”之前选择了“不匹配的面孔”,但是没有任何反应。在点击后选择“不匹配”的话可以正常工作,并且会出现另一个弹窗,其中包含“再试一次”按钮,这个按钮可以正常工作。不知道是模拟器的问题还是因为我这里没有真实设备。 - maphongba008
这是模拟器的问题。您的代码在真实设备上应该正常运行。 - loup4
它在我的iPhoneX上无法工作。我无法在真实设备上陷入LAError.authenticationFailed的情况。 - Giuseppe Roberto Rossi
1
在运行 iOS 14 的模拟器设备上,Xcode 12.0.1 中无法正常工作。 - alstr
仍然无法在 Xcode 12.5 中使用 iPad OS 14.5 模拟器运行。 - Manu Mateos

0

答案: 我也遇到了同样的问题。然后我在设备上检查了一下,它可以正常工作。所以这只是在模拟器中不起作用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接