ios - 检查 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 但启用了密码,它也不会提供有关密码的信息。因为我需要显示启用的类型是“密码”。有什么办法可以做到这一点?
解决方案
你必须使用LAPolicy.deviceOwnerAuthenticationWithBiometrics
.
根据苹果文档:
如果 Touch ID 或 Face ID 不可用或未注册,则策略评估失败。在连续 3 次不成功的 Touch ID 或 Face ID 尝试后,策略评估将失败。五次尝试失败后,Touch ID 和 Face ID 身份验证均被禁用,需要用户输入设备密码才能重新启用。
LAPolicy.deviceOwnerAuthentication启用:
如果 Touch ID 或 Face ID 可用、已注册且未禁用,则首先会要求用户提供该信息。否则,将要求他们输入设备密码。如果未启用设备密码,则策略评估失败。密码验证在 6 次尝试失败后被禁用,并且它们之间的延迟逐渐增加。
推荐阅读
- reactjs - 添加 takeUntil 停止触发所有动作 rxJS, redux-observable
- python - Pandas:跨多列获取一行中指定值的频率
- java - 我应该如何将对象添加到私有静态 ArrayList?
- testing - 无法在 Testcafe 中单击弹出确认按钮
- django - 尝试使用 manage.py 时没有名为“djangosite”的模块
- html - 标签宽度适合标签文本
- reactjs - React Native - 使用我制作的自定义组件时出错(错误:未定义无法解析模块
) - jenkins - 如何在 jenkins slave 中查看构建控制台输出
- python - 通过循环不断更新QTableView
- node.js - kraken.js 没有创建新项目