TouchID、FaceID认证的基本使用

1、Info.plist新增描述(此项针对FaceID)

<key>NSFaceIDUsageDescription</key>
<string>需要使用您的面容识别进行安全认证</string>
<key>UIApplicationSceneManifest</key>


2、导入库

import LocalAuthentication


3、写一个函数,实现如下代码

现在应该不会还有支持8.0一下的App了吧?如果有请自行添加判断

let context = LAContext()
var error: NSError?
if (context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics,
error: &error)) {
    context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics,
localizedReason: "验证") { access, resError in
        if (access) {
            print("验证成功了")
        } else {
            guard let error = resError as? LAError else { return }
            print("error is", error)
        }
    }
}


4、iOS 11以后

可以通过LAContext().biometryType进行进一步的判断,但需要注意,需要在canEvaluatePolicy之后

@available(iOS 11.0, *)
public enum LABiometryType : Int {

    /// The device does not support biometry.
    @available(iOS 11.2, *)
    case none = 0

    /// The device does not support biometry.
    @available(iOS, introduced: 11.0, deprecated: 11.2, renamed: "LABiometryType.none")
    public static var LABiometryNone: LABiometryType { get }

    /// The device supports Touch ID.
    case touchID = 1

    /// The device supports Face ID.
    case faceID = 2
}

·

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容