2016-07-14 35 views
7

我在许多地方描述了LocalAuthentication的以下实现。TouchID activateTouchWithResponse在不请求指纹的情况下返回成功

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 :,答复:)返回成功,而不要求任何指纹。 我实际上使用UISwitch启用或禁用TouchID,因此在禁用并重新启用后,我想重新进行身份验证并重新输入指纹。

为什么它缓存身份验证?

谢谢

+0

添加错误,如果看到会发生什么。 – Konsy

+0

错误为零。我第二次评估该政策时,我得到的成功和错误为零,没有被提示触摸按钮。 –

+0

尝试做,如果错误!=零而不是 – Konsy

回答

14

LAContext,一旦评估,将返回成功,直到它被释放。您可以手动使其无效,然后返回的错误将是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() 
    }) 
}) 
+0

很好的答案,它清除了情况,谢谢。 Apple文档中没有任何关于它的文字和短语: “不要以为以前成功的政策评估意味着未来的评估也会成功,政策评估可能由于各种原因而失败,包括用户取消或系统。“ 确实令人困惑 – Dren

3

由于IOS 9有touchIDAuthenticationAllowableReuseDuration用于上下文

的时间,为触摸ID认证重用是允许的。 如果在指定的时间间隔内使用Touch ID成功验证设备,则接收者的验证会自动成功,而不会提示用户输入Touch ID。 默认值为0,这意味着Touch ID身份验证无法重复使用。 Touch ID身份验证重用的最长允许持续时间由LATouchIDAuthenticationMaximumAllowableReuseDuration常量指定。通过将此属性设置为大于此常数的值,您无法指定更长的持续时间。 可用性的iOS(9.0和更高版本),MACOS(10.12及更高版本)

如果例如设置为60

context.touchIDAuthenticationAllowableReuseDuration = 60 

它会自动而不检查成功,如果用户已顺利通过触摸身份证检查在最后60秒。

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

0

我面临同样的问题,但是后来我增加了如下的持续时间值:

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

该解决方案为我工作。

+0

您可以使用常量LATouchIDAuthenticationMaximumAllowableReuseDuration,因为它是最大允许时间间隔,其值仅为5分钟。 –

相关问题