我们怎么知道应用程序已经启用了 touch 或 face id?现在我正在使用 Biometric Authentication 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 获得,它会告诉您有关设备 biometryType 的信息,您还可以调用 authenticate 方法。如果它返回错误,您可以将其转换为 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
}