2

实际上,我构建了一个包含本地身份验证的应用程序。

到目前为止我的代码:

func authenticateUser() {
        let authenticationContext = LAContext()
        var error: NSError?
        let reasonString = "Touch the Touch ID sensor to unlock."

        // Check if the device can evaluate the policy.
        if authenticationContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error) {

            authenticationContext.evaluatePolicy( .deviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString, reply: { (success, evalPolicyError) in

                if success {
                    print("success")
                } else {
                    if let evaluateError = error as NSError? {
                        // enter password using system UI 
                    }

                }
            })

        } else {
            print("toch id not available")
           // enter password using system UI
        }
    }

我的问题是当应用程序没有触摸 ID 或无效指纹时,我想使用密码锁定场景。

如下图:

在此处输入图像描述

我该怎么做?

4

2 回答 2

10

您应该使用.deviceOwnerAuthentication而不是.deviceOwnerAuthenticationWithBiometrics评估策略。使用此参数,如果可用,系统将使用生物特征验证,否则它会显示密码屏幕。如果生物特征验证可用但失败,则后备按钮将重定向到密码屏幕。请参阅文档

如果 Touch ID 或 Face ID 可用、已注册且未禁用,则首先会要求用户提供该信息。否则,将要求他们输入设备密码。

点击后备按钮会切换身份验证方法以要求用户输入设备密码。

所以你的代码将是:

func authenticateUser() {
        let authenticationContext = LAContext()
        var error: NSError?
        let reasonString = "Touch the Touch ID sensor to unlock."

        // Check if the device can evaluate the policy.
        if authenticationContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthentication, error: &error) {

            authenticationContext.evaluatePolicy( .deviceOwnerAuthentication, localizedReason: reasonString, reply: { (success, evalPolicyError) in

                if success {
                    print("success")
                } else {
                    // Handle evaluation failure or cancel
                }
            })

        } else {
            print("passcode not set")
        }
    }
于 2019-01-22T09:22:51.177 回答
2

此时,恐怕您无法在您的应用程序中访问此密码锁定屏幕,它与iOS本身有关。您可能需要构建自己的自定义视图控制器以看起来/表现为密码锁定场景(使用 Touch ID)。我个人建议使用一个库来实现这一点,我已经尝试过PasscodeLock,它对我来说效果很好。

于 2017-07-06T07:19:33.203 回答