【问题标题】:Enabled touch id and face id in the app在应用中启用 touch id 和 face id
【发布时间】:2018-08-07 18:15:59
【问题描述】:

我们怎么知道应用程序已经启用了 touch 或 face id?现在我正在使用 Biometric Authentication CocoPod 来集成它。

提前致谢

【问题讨论】:

    标签: ios iphone touch-id face-id


    【解决方案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
    }
    

    【讨论】:

    • 虽然不错...我更关心如何知道我的应用是否启用了触摸或面部 id
    猜你喜欢
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 2022-09-29
    相关资源
    最近更新 更多