2016-07-14 70 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分鐘。 –

相關問題