TouchID、FaceID认证的基本使用

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

NSFaceIDUsageDescription
需要使用您的面容识别进行安全认证
UIApplicationSceneManifest


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
}

·

你可能感兴趣的:(TouchID、FaceID认证的基本使用)