说明
iPhone X 以后支持Face ID, iPhone 5S以后支持Touch ID. 怎么准确判断机器是否支持,Face ID或者Touch ID呢? 实际上系统有方法判断,但是如果没有注册Face ID或者Touch ID,判断结果也是none
解决方法
用swift 5来解决
import Foundation
import LocalAuthentication
extension LAContext {
enum BiometricType: String {
case none
case touchID
case faceID
}
var biometricType: BiometricType {
var error: NSError?
guard self.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) else {
// Capture these recoverable error through fabric
return .none
}
if #available(iOS 11.0, *) {
switch self.biometryType {
case .touchID:
return .touchID
case .faceID:
return .faceID
default:
return .none
}
}
return self.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) ? .touchID : .none
}
}
调用方法
// need to import LocalAuthentication in the calling file
// import LocalAuthentication
let currentType = LAContext().biometricType
print("biometry type > \(currentType)")
// biometry type > touchID
如果用模拟器来调试,需要开启Touch ID或者Face ID已经注册。设置路径如下
Simulator > Hardware > Touch ID/Face ID > Enrolled.
参考
https://stackoverflow.com/questions/46887547/how-to-programmatically-check-support-of-face-id-and-touch-id/62299672#62299672