ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2020-01-13 19:02:36  阅读:786  来源: 互联网

标签:err 解锁 LAError Face Authentication notifyUser TouchId FaceId ID


说明

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.
    在这里插入图片描述
  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

在这里插入图片描述
5. 第一次打开的时候会弹出确认框
在这里插入图片描述
6. 点击OK后就弹出,验证Face ID的界面
在这里插入图片描述
7. 点击确认 Simulator > Hardware > Face ID/ Touch ID > Matching Face
在这里插入图片描述
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

程序员易筋 发布了127 篇原创文章 · 获赞 12 · 访问量 2万+ 私信 关注

标签:err,解锁,LAError,Face,Authentication,notifyUser,TouchId,FaceId,ID
来源: https://blog.csdn.net/zgpeace/article/details/103962124

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有