首页 > 解决方案 > 检查 FaceID 是否启用

问题描述

我想获取设备中当前使用的锁类型的字符串,无论是 FaceID、touchID 还是 PassCode。以下是我的代码:-

func getBiometricType() -> String {
    var biometricType: Global.BiometricType {
        let context = LAContext()
        var error: NSError?
        guard context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) else { return .none }
        if #available(iOS 11.0, *) {
            switch context.biometryType {
                case .touchID:
                    return .touchID
                case .faceID:
                    return .faceID
                case .none:
                    return .none
            }
        } else {
            guard context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) else { return .none }
            return context.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil) ?
     .touchID : .PIN
        }
    }
    return biometricType.rawValue
}

但是这个 canEvaluatePolicy 只检查设备是否支持生物识别。即使尚未设置 FaceID 但启用了密码,它也不会提供有关密码的信息。因为我需要显示启用的类型是“密码”。有什么办法可以做到这一点?

标签: iosswiftbiometricsface-idpasscode

解决方案


你必须使用LAPolicy.deviceOwnerAuthenticationWithBiometrics.

根据苹果文档:

如果 Touch ID 或 Face ID 不可用或未注册,则策略评估失败。在连续 3 次不成功的 Touch ID 或 Face ID 尝试后,策略评估将失败。五次尝试失败后,Touch ID 和 Face ID 身份验证均被禁用,需要用户输入设备密码才能重新启用。

LAPolicy.deviceOwnerAuthentication启用:

如果 Touch ID 或 Face ID 可用、已注册且未禁用,则首先会要求用户提供该信息。否则,将要求他们输入设备密码。如果未启用设备密码,则策略评估失败。密码验证在 6 次尝试失败后被禁用,并且它们之间的延迟逐渐增加。


推荐阅读