美文网首页
TouchID、FaceID认证的基本使用

TouchID、FaceID认证的基本使用

作者: Codepgq | 来源:发表于2021-11-22 16:41 被阅读0次

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
}

·

相关文章

网友评论

      本文标题:TouchID、FaceID认证的基本使用

      本文链接:https://www.haomeiwen.com/subject/myybtrtx.html