我们怎么知道应用程序已经启用了触摸或面部识别ID?现在我正在使用生物识别身份验证CocoPod来集成它。
提前致谢
您可以将 LocalAuthentication 与 LAContext 一起使用,它将完成这项工作并告诉您有关设备生物测量状态的所有信息。您可以使用此单例类作为起点:
import LocalAuthentication
final public class BiometryManager {
public typealias SuccessComplition = () -> Void
public typealias ErrorComplition = (Error?) -> Void
public static let shared = BiometryManager()
private let context = LAContext()
private init() { }
public var biometryType: LABiometryType {
var error: NSError?
guard context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) else {
return LABiometryType.LABiometryNone
}
return context.biometryType
}
public func authenticate(successComplition: @escaping SuccessComplition, errorComplition: @escaping ErrorComplition) {
var error: NSError?
let reasonString = "provide reason text"
guard context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) else {
errorComplition(error)
return
}
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString, reply: { (success, evalPolicyError) in
DispatchQueue.main.async {
if success {
successComplition()
} else {
errorComplition(evalPolicyError)
}
}
})
}
}
该类可从iOS 11获得,它将告诉您有关设备生物测量类型的信息,您还可以调用身份验证方法。如果它返回错误,您可以将其强制转换为 LAError 并从中获取更具体的错误代码。希望对您有所帮助。
看看: https://developer.apple.com/documentation/localauthentication/laerror
您可以将此属性添加到上述类中以检查生物计量可用性:
public var isAvailable: Bool {
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {return true}
guard let laError = error as? LAError else {return false}
// Check the laError.code, maybe its locked or something else and make specific decision
}