Swift TouchId指纹解锁,FaceId面部解锁

说明

TouchId指纹识别,FaceId面部解锁,统称为生物识别。

实现

  1. 引入Local Authentication Framework,如果是iOS 13,默认就有,不用重新引入。
    引入头文件 import LocalAuthentication
  2. 检查生物识别是否可用
let context = LAContext()

var error: NSError?

if context.canEvaluatePolicy(
	LAPolicy.DeviceOwnerAuthenticationWithBiometrics, 
		error: &error) {
	// Biometry is available on the device
} else {
	// Biometry is not available on the device
	// No hardware support or user has not set up biometric auth
}

上面是验证生物识别是否可用,如果不可用。
错误的原因如下:

  • LAError.biometryNotEnrolled - 用户没有注册生物识别信息(没有录入指纹,或者录入面部识别信息).
  • LAError.passcodeNotSet - 用户没有设置密码.
  • LAError.biometryNotAvailable - 该设备硬件不支持生物识别.
  1. 如果可用,则可以校验用户信息。
func notifyUser(_ msg: String, err: String?)  {
        print("msg > \(msg)")
        print("err > \(err)")
    }

func authorizeBiometrics(_ context: LAContext) {
        // Device can use biometric authentication
        context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Access requires authentication") { (success, error) in
            if let err = error {
                switch err._code {
                case LAError.Code.systemCancel.rawValue:
                    self.notifyUser("Session cancelled", err: err.localizedDescription)
                case LAError.Code.userCancel.rawValue:
                    self.notifyUser("Please try again", err: err.localizedDescription)
                case LAError.Code.userFallback.rawValue:
                    self.notifyUser("Authentication", err: "Password option selected")
                // Custom Code to obtain password here
                default:
                    self.notifyUser("Authentication failed", err: error?.localizedDescription)
                }
            } else {
                //                    self.notifyUser("Authentication Successful", err: "You now have full access")
                if (context.biometryType == LABiometryType.faceID) {
                    // Device support Face ID
                    self.notifyUser("Authentication Successful", err: "Device support Face ID")
                } else if context.biometryType == LABiometryType.touchID {
                    // Device supports Touch ID
                    self.notifyUser("Authentication Successful", err: "Device supports Touch ID")
                } else {
                    // Device has no biometric support
                    self.notifyUser("Authentication Successful", err: "Device has no biometric support")
                }
            }
        }
    }

校验成功则打印信息:

  • LABiometryType.faceID 面部识别成功
  • LABiometryType.touchID 指纹识别成功
  • 其它就是硬件不支持。

检验识别错误,标识为error不为空

  • LAError.systemCancel - 授权过程当中被系统取消。特别容易发生在,App推到后台background.
  • LAError.userCancel - 授权被用户取消.
  • LAError.userFallback - 用户选择用密码,而不是用Touch ID或者Face ID.

代码下载

笔者用SwiftUI实现,下载地址:
https://github.com/zgpeace/iOSBiometrics.git

运行代码

  1. 如果没有启用Touch ID或者Face ID信息,则打印信息如下。

Biometry is not available on the device
No hardware support to user has not set up biometric auth
msg > User is not enrolled
err > Optional(“No identities are enrolled.”)

  1. 模拟器也可以开启FaceID 或者 Touch ID。 Simulator > Hardware > Face ID / Touch ID > Enrolled.
    Swift TouchId指纹解锁,FaceId面部解锁
  2. 运行报错信息如下:

Biometry is available on the device
2020-01-13 17:54:00.881269+0800 Biometrics[36054:652894] [access] This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app’s Info.plist must contain an NSFaceIDUsageDescription key with a string value explaining to the user how the app uses this data.

  1. 需要在target > Info > Custom iOS Target Priorities > 添加Key Value
key :
Privacy - Face ID Usage Description
value:
This app uses Face ID to confirm your identity

Swift TouchId指纹解锁,FaceId面部解锁
5. 第一次打开的时候会弹出确认框
Swift TouchId指纹解锁,FaceId面部解锁
6. 点击OK后就弹出,验证Face ID的界面
Swift TouchId指纹解锁,FaceId面部解锁
7. 点击确认 Simulator > Hardware > Face ID/ Touch ID > Matching Face
Swift TouchId指纹解锁,FaceId面部解锁
8. 控制台打印结果如下:

Biometry is available on the device
msg > Authentication Successful
err > Optional("Device support Face ID")

参考

https://www.techotopia.com/index.php/Implementing_TouchID_Authentication_in_iOS_8_Apps

Swift TouchId指纹解锁,FaceId面部解锁Swift TouchId指纹解锁,FaceId面部解锁 程序员易筋 发布了127 篇原创文章 · 获赞 12 · 访问量 2万+ 私信 关注
上一篇:php-Laravel 5.1-注册用户应用程序时重定向到/ dashboard,但不检查帐户是否已确认


下一篇:常见英文缩写词