LocalAuthentication框架 - 如何在第一次调用时显示“输入密码”

时间:2016-04-06 21:42:56

标签: ios objective-c swift touch-id

我正在使用iOS的LocalAuthentication框架,并遵循网络上的常规教程为我的应用程序实现TouchID身份验证。

当应用程序调用context.evaluatePolicy(策略,错误:&错误)时,我想向用户显示“输入密码”的选项,而不是让用户选择“取消”以关闭对话框并输入密码。

这是AppStore应用中的默认行为,但我无法让我的应用以相同的方式执行。请参阅下面的AppStore截图:

AppStore TouchID Authentication

我使用的代码与各种教程一致。见下面的代码。

我的应用程序启动时显示以下屏幕:

My App TouchID authentication

我在SO和其他网站上搜索过高和低,但是无法使用“显示密码”启动我的应用程序。当我使用未注册的手指来验证LA对话框时,请更改为“再试一次”并显示“显示密码”按钮。

        let context = LAContext()
        var error : NSError?

        // Test if TouchID policy is available on the device and a fingerprint has been enrolled.
        var policy : LAPolicy!
        if #available(iOS 9.0, *) {
            policy = (context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error:&error) ? LAPolicy.DeviceOwnerAuthenticationWithBiometrics : LAPolicy.DeviceOwnerAuthentication)
        } else {
            // Fallback on earlier versions
            policy = LAPolicy.DeviceOwnerAuthenticationWithBiometrics
        }
        if context.canEvaluatePolicy(policy, error:&error) {
            // evaluate
            context.evaluatePolicy(policy, localizedReason: reason, reply: {
                (success: Bool, authenticationError: NSError?) -> Void in

                // check whether evaluation of fingerprint was successful
                if success {
                    okHandler()
                } else {
                    // fingerprint validation failed
                    // get the reason for validation failure
                    var failureReason = "unable to authenticate user"
                    if let code = error?.code {
                        switch code {
                        case LAError.AuthenticationFailed.rawValue:
                            failureReason = "authentication failed"
                        case LAError.UserCancel.rawValue:
                            failureReason = "user canceled authentication"
                        case LAError.SystemCancel.rawValue:
                            failureReason = "system canceled authentication"
                        case LAError.PasscodeNotSet.rawValue:
                            failureReason = "passcode not set"
                        case LAError.UserFallback.rawValue:
                            failureReason = "user chose password"
                        default:
                            failureReason = "unable to authenticate user"
                        }
                    }
                    print("validation reason: \(failureReason).");
                    cancelHandler()
                }
            })
        } else {
        }

请帮忙!

2 个答案:

答案 0 :(得分:3)

你做不到。用户必须至少输入一次错误的指纹才能显示后备按钮。希望苹果在功能中添加属性。

答案 1 :(得分:1)

不幸的是,您无法做到这一点,出于安全和隐私原因,本地身份验证框架非常有限。