ios - 如何在我的应用程序中使用密码锁定场景?

标签 ios swift xcode authentication passcode

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

到目前为止我的代码:

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 或无效指纹时,我想使用密码锁定场景。

如下图:

enter image description here

我该怎么做?

最佳答案

您应该使用 .deviceOwnerAuthentication 而不是 .deviceOwnerAuthenticationWithBiometrics 来评估策略。使用此参数,系统将使用生物识别身份验证(如果可用),否则它会显示密码屏幕。如果生物识别身份验证可用但失败,则后备按钮会重定向到密码屏幕。参见 documentation :

If Touch ID or Face ID is available, enrolled, and not disabled, the user is asked for that first. Otherwise, they are asked to enter the device passcode.

Tapping the fallback button switches the authentication method to ask the user for the device passcode.

所以你的代码将是:

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")
        }
    }

关于ios - 如何在我的应用程序中使用密码锁定场景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44941863/

相关文章:

ios - 从 xcode 中单元格内的文本字段获取值

ios - 使用迦太基在 Xcode 中导入 ZipFoundation 不起作用

ios - Git 克隆后 XCode 项目位置错误

ios - 为什么在iOS中使用核心图形绘制的线的连接处会出现一个点?

ios - 自定义 XML 解析器 - 当某些键不存在时该怎么办?

ios - 在 IBDesignable View 中重写以便它显示在 Interface Builder 中的正确方法是什么?

swift - 无法将协议(protocol)的通用关联类型的值转换为预期的参数类型

swift - 错误信息 Could not cast value type error - Swift

ios - 如何使用 Swift iOS 监听 UIReturnKeyType.Next

SwiftUI - 结构绑定(bind)未按预期更新 UI