Explain
TouchId fingerprint recognition, FaceId face unlocking, collectively referred to as biometrics.
Realization
- Introduce the Local Authentication Framework. If it's iOS 13, it's available by default. You don't need to re introduce it.
import LocalAuthentication - Check that biometrics are available
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 }
Above is to verify that biometrics is available, if not.
The reasons for the error are as follows:
- Laerror. Biometric not registered - user has not registered biometric information (fingerprint or facial)
- LAError.passcodeNotSet - password not set by user
- LAError.biometryNotAvailable - the device hardware does not support biometrics
- If available, user information can be verified.
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") } } } }
If the verification is successful, the information will be printed:
- Labiometerytype.faceid face recognition succeeded
- Labiometerytype.touchid fingerprint recognition succeeded
- Others are not supported by hardware.
Inspection identification error, error is not empty
- LAError.systemCancel - cancelled by the system during the authorization process. It's especially easy to happen when the App is pushed to the background
- LAError.userCancel - authorization cancelled by user
- LAError.userFallback - user chooses to use password instead of Touch ID or Face ID
Code download
The author implements it with SwiftUI, download address:
https://github.com/zgpeace/iOSBiometrics.git
Run code
- If Touch ID or Face ID information is not enabled, the print information is as follows.
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.")
- The simulator can also turn on FaceID or touch ID. Simulator > Hardware > Face ID / Touch ID > Enrolled.
- The operation error message is as follows:
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.
- You need to add key values in target > info > Custom IOS target priorities
key : Privacy - Face ID Usage Description value: This app uses Face ID to confirm your identity
5. A confirmation box will pop up when opening for the first time
6. Click OK to open the interface to verify Face ID
7. Click to confirm simulator > Hardware > face ID / touch ID > matching face
8. The printing results of the console are as follows:
Biometry is available on the device msg > Authentication Successful err > Optional("Device support Face ID")
Reference resources
https://www.techotopia.com/index.php/Implementing_TouchID_Authentication_in_iOS_8_Apps