【问题标题】:Setting the TouchID "Enter Password" fallback to start editing UITextField设置 TouchID“输入密码”后备以开始编辑 UITextField
【发布时间】:2015-06-15 00:47:00
【问题描述】:

我设置了 touchID,并在我的应用程序中正常工作。

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

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

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

我想关闭 touchID alertview 并让我的 passwordTextField 成为 firstResponder。

我自然试过了:

self.passwordTextField.becomeFirstResponder()

但这会导致错误:

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
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.

Exception: Only run on the main thread!

这是我的身份验证功能:

func requestFingerprintAuthentication() {
    // Get the local authentication context.
    let context = LAContext()

    // Declare a NSError variable.
    var error: NSError?

    // Set the reason string that will appear on the authentication alert.
    var reasonString = "Sign In."

    // Check if the device can evaluate the policy.
    if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &error) {
        [context .evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString, reply: { (success: Bool, evalPolicyError: NSError?) -> Void in

            if success {
                NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
                    println("successfull signin with touchID")
                    self.emailTextField.text = emailID as! String
                    self.passwordTextField.text = passwordID as! String
                    self.signIn(self.signInButton)
                })
            }
            else{
                // If authentication failed then show a message to the console with a short description.
                // In case that the error is a user fallback, then show the password alert view.
                println(evalPolicyError?.localizedDescription)

                switch evalPolicyError!.code {

                case LAError.SystemCancel.rawValue:
                    println("Authentication was cancelled by the system")

                case LAError.UserCancel.rawValue:
                    println("Authentication was cancelled by the user")

                case LAError.UserFallback.rawValue:
                    println("User selected to enter custom password")
                    self.passwordTextField.becomeFirstResponder()

                default:
                    println("Authentication failed")
                    self.passwordTextField.becomeFirstResponder()
                }
            }

        })]
    }
    else{
        // If the security policy cannot be evaluated then show a short message depending on the error.
        switch error!.code{

        case LAError.TouchIDNotEnrolled.rawValue:
            println("TouchID is not enrolled")

        case LAError.PasscodeNotSet.rawValue:
            println("A passcode has not been set")

        default:
            // The LAError.TouchIDNotAvailable case.
            println("TouchID not available")
        }

        // Optionally the error description can be displayed on the console.
        println(error?.localizedDescription)

        // Show the custom alert view to allow users to enter the password.
        //self.showPasswordAlert()
        self.passwordTextField.becomeFirstResponder()
    }
}

打印出来了

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

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

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

【问题讨论】:

  • 你试过把 becomeFirstResponder 放在主线程上吗?
  • @PaulCezanne 不,我没有,你会怎么做?
  • @PaulCezanne 我花了一段时间才弄清楚。但你的建议奏效了,谢谢:)
  • 我很高兴它做到了!您应该将我的答案标记为正确,以供将来的搜索者使用,而且您和我都会获得声誉积分。 (见stackoverflow.com/help/whats-reputation)欢迎来到!
  • @MarkL 您能否根据 Paul Cezanne 的建议更新您上面的代码,并使用更新的代码标记已编辑?

标签: ios swift touch-id


【解决方案1】:

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

你的情况有点不同,但是控制台中的日志给出了答案:“Exception: Only run on the main thread!”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多