TouchID activateTouchWithResponse returns 成功而不请求指纹

TouchID activateTouchWithResponse returns success without requesting fingerprint

如许多地方所述,我有以下 LocalAuthentication 实现。

context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: "Logging in with Touch ID", reply: { (success : Bool, error : NSError? ) -> Void in
        dispatch_async(dispatch_get_main_queue(), {

        if success {
            let alert = UIAlertController(title: "Success", message: "", cancelButtonTitle: "Great!")
            self.presentViewController(alert, animated: true, completion: nil)
        }

        if let error = error {
            var message :String

            switch(error.code) {
            case LAError..AuthenticationFailed:
                message = "There was a problem verifying your identity."
            case LAError..UserCancel:
                message = "You pressed cancel."
            case LAError..UserFallback:
                message = "You pressed password."
            default:
                message = "Touch ID may not be configured"
            }

            let alert = UIAlertController(title: "Error", message: message, cancelButtonTitle: "Darn!")
            self.presentViewController(alert, animated: true, completion: nil)
        }
    })
})

但是在我成功地使用我的指纹进行身份验证后,然后 evaluatePolicy(, localizedReason:, reply:) returns 成功而无需请求任何指纹。 我实际上是通过 UISwitch 启用或禁用 TouchID,因此在禁用并重新启用后,我想重新验证并重新输入我的指纹。

为什么要缓存身份验证?

谢谢

LAContext 一旦被评估,将return 成功,直到它被释放。您可以手动使其无效,然后 returned 错误将是 LAError.InvalidContext.

如果你想每次都提示TouchID确认,你需要每次都创建一个LAContext。这可以实现

context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: "Logging in with Touch ID", reply: { (success : Bool, error : NSError? ) -> Void in
        dispatch_async(dispatch_get_main_queue(), {

        if success {
            let alert = UIAlertController(title: "Success", message: "", cancelButtonTitle: "Great!")
            self.presentViewController(alert, animated: true, completion: nil)
        }

        if let error = error {
            var message :String

            switch(error.code) {
            case LAError..AuthenticationFailed:
                message = "There was a problem verifying your identity."
            case LAError..UserCancel:
                message = "You pressed cancel."
            case LAError..UserFallback:
                message = "You pressed password."
            default:
                message = "Touch ID may not be configured"
            }

            let alert = UIAlertController(title: "Error", message: message, cancelButtonTitle: "Darn!")
            self.presentViewController(alert, animated: true, completion: nil)
        }

        context = LAContext()
    })
})

因为 ios 9 有 touchIDAuthenticationAllowableReuseDuration 上下文

The duration for which Touch ID authentication reuse is allowable. If the device was successfully authenticated using Touch ID within the specified time interval, then authentication for the receiver succeeds automatically, without prompting the user for Touch ID. The default value is 0, meaning that Touch ID authentication cannot be reused. The maximum allowable duration for Touch ID authentication reuse is specified by the LATouchIDAuthenticationMaximumAllowableReuseDuration constant. You cannot specify a longer duration by setting this property to a value greater than this constant. Availability iOS (9.0 and later), macOS (10.12 and later)

例如,如果您设置为 60

context.touchIDAuthenticationAllowableReuseDuration = 60

如果用户在最近60秒内成功通过touch id校验,则不校验自动成功。

因此,您可以设置为适合您的值。我发现它非常好,要求用户在他几秒钟前刚刚触摸时再次触摸是很烦人的。(例如解锁屏幕)。

我遇到了同样的问题,但后来我增加了持续时间值,如下所示:

context.touchIDAuthenticationAllowableReuseDuration = Double(5 * 60) // 5 min, 

这个解决方案对我有用。