判断iOS机器是否支持TouchId, FaceId

说明

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值