在应用中启用了触摸ID和面部ID

时间:2018-08-07 18:16:00

标签: ios iphone touch-id face-id

我们怎么知道该应用程序已经启用了触摸或面部识别?现在,我正在使用生物特征认证CocoPod进行集成。

预先感谢

1 个答案:

答案 0 :(得分:1)

您可以将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
}