【问题标题】:Touch ID on success set a variableTouch ID 成功设置变量
【发布时间】:2016-05-06 19:32:11
【问题描述】:

我正在尝试根据 touch id 的成功/失败返回 true 或 false。但是当成功 if 条件被调用时,函数以假值退出。我是 iOS 开发的新手。我相信我应该使用某种完成处理程序,但在这种特定情况下我不明白如何使用。

func authenticateUser(reasonString: String) -> Bool {
    // Get the local authentication context.
    let context = LAContext()

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

    // Current authorization status of user
    var isAuthorized = false

    // 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 {
                isAuthorized = true
            }
            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.
                print(evalPolicyError?.localizedDescription)

                switch evalPolicyError!.code {

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

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

                case LAError.UserFallback.rawValue:
                    print("User selected to enter custom password")

                default:
                    print("Authentication failed")

                }
            }

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

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

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

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

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

    }
return isAuthorized
}

即使在执行 touchID 部分之前,此函数也会返回 false。有人可以指导一下吗?

【问题讨论】:

    标签: swift2 touch-id


    【解决方案1】:

    您的函数 authenticateUser 将始终返回 false,因为您返回设置为 false 的 isAuthorized 值。 context.canEvaluatePolicy 和 context.evaluatePolicy 仍在执行,isAuthorized 的值没有改变。

    实际流程是这样的:

    设置 isAuthorized== false

    {call context.canEvaluatePolicy --> 仍在执行 调用 context.evaluatePolicy --> 在 context.canEvaluatePolicy 成功时调用}

    Return isAuthorized = False --> context.canEvaluatePolicy 和 context.evaluatePolicy 仍在执行。

    即使块方法正在执行,执行流程也会完成方法执行。

    我的建议是从函数末尾删除 return isAuthorized 并实现成功和失败块。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-12
      • 1970-01-01
      • 2019-06-24
      • 2021-11-17
      相关资源
      最近更新 更多