ios – 设置TouchID“输入密码”后备以开始编辑UITextField

前端之家收集整理的这篇文章主要介绍了ios – 设置TouchID“输入密码”后备以开始编辑UITextField前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的应用程序中设置了touchID并正常工作.

但是,我想更改“输入密码”的功能.

创建我的身份验证时,我遵循以下教程:http://www.appcoda.com/touch-id-api-ios8/

但是,他们使用alertView作为“输入密码”选项.

我想解雇touchID alertview并让我的passwordTextField成为第一个响应者.

当然我试过了:

  1. self.passwordTextField.becomeFirstResponder()

但这会导致错误

  1. 2015-04-09 10:48:42.309 Formula Stocks[3933:964941] *** Assertion failure in void _UIPerformResizeOfTextViewForTextContainer(NSLayoutManager *,UIView<NSTextContainerView> *,NSTextContainer *,NSUInteger)(),/SourceCache/UIFoundation/UIFoundation-371.13/UIFoundation/TextSystem/NSLayoutManager_Private.m:1547
  2. 2015-04-09 10:48:42.312 Formula Stocks[3933:964941] <NSXPCConnection: 0x1701061e0> connection to service named com.apple.CoreAuthentication.daemon: Warning: Exception caught during decoding of received reply to message 'evaluatePolicy:options:reply:',dropping incoming message and calling failure block.
  3.  
  4. Exception: Only run on the main thread!

这是我的身份验证功能

  1. func requestFingerprintAuthentication() {
  2. // Get the local authentication context.
  3. let context = LAContext()
  4.  
  5. // Declare a NSError variable.
  6. var error: NSError?
  7.  
  8. // Set the reason string that will appear on the authentication alert.
  9. var reasonString = "Sign In."
  10.  
  11. // Check if the device can evaluate the policy.
  12. if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics,error: &error) {
  13. [context .evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics,localizedReason: reasonString,reply: { (success: Bool,evalPolicyError: NSError?) -> Void in
  14.  
  15. if success {
  16. NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
  17. println("successfull signin with touchID")
  18. self.emailTextField.text = emailID as! String
  19. self.passwordTextField.text = passwordID as! String
  20. self.signIn(self.signInButton)
  21. })
  22. }
  23. else{
  24. // If authentication Failed then show a message to the console with a short description.
  25. // In case that the error is a user fallback,then show the password alert view.
  26. println(evalPolicyError?.localizedDescription)
  27.  
  28. switch evalPolicyError!.code {
  29.  
  30. case LAError.SystemCancel.rawValue:
  31. println("Authentication was cancelled by the system")
  32.  
  33. case LAError.UserCancel.rawValue:
  34. println("Authentication was cancelled by the user")
  35.  
  36. case LAError.UserFallback.rawValue:
  37. println("User selected to enter custom password")
  38. self.passwordTextField.becomeFirstResponder()
  39.  
  40. default:
  41. println("Authentication Failed")
  42. self.passwordTextField.becomeFirstResponder()
  43. }
  44. }
  45.  
  46. })]
  47. }
  48. else{
  49. // If the security policy cannot be evaluated then show a short message depending on the error.
  50. switch error!.code{
  51.  
  52. case LAError.TouchIDNotEnrolled.rawValue:
  53. println("TouchID is not enrolled")
  54.  
  55. case LAError.PasscodeNotSet.rawValue:
  56. println("A passcode has not been set")
  57.  
  58. default:
  59. // The LAError.TouchIDNotAvailable case.
  60. println("TouchID not available")
  61. }
  62.  
  63. // Optionally the error description can be displayed on the console.
  64. println(error?.localizedDescription)
  65.  
  66. // Show the custom alert view to allow users to enter the password.
  67. //self.showPasswordAlert()
  68. self.passwordTextField.becomeFirstResponder()
  69. }
  70. }

它会打印出来

  1. Optional("Fallback authentication mechanism selected.")
  2. User selected to enter custom password

所以我知道它正在调用正确的.case

任何帮助它选择我的UITextField将不胜感激!

解决方法

您需要在主线程上放置becomeFirstResponder. iOS要求所有UI操作都在主线程上.在使用闭包时(Objective-C中的块),您经常会在主线程上意外地关闭UI.你得到的常见错误是UI“冻结”了几秒钟.这是你需要时不在主线程上的经典案例.

您的情况略有不同,但控制台中的日志给出了答案:“例外:仅在主线程上运行!”

猜你在找的iOS相关文章