Explanation
TouchId fingerprint recognition, FaceId Face Unlock, collectively known as biometrics.
achieve
- The introduction of Local Authentication Framework, if it is iOS 13, there is a default, without re-introduction.
The introduction of header filesimport LocalAuthentication
- Check Biometric is 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
}
The above is the biometric authentication is available, if not available.
The wrong reasons as follows:
- LAError.biometryNotEnrolled - user is not registered biometric information (fingerprint not recorded, or the entry face recognition information).
- LAError.passcodeNotSet - you do not set a password.
- LAError.biometryNotAvailable - the device hardware does not support biometrics.
- If available, the user can verify the information.
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")
}
}
}
}
Check the success of the print information:
- LABiometryType.faceID facial recognition success
- LABiometryType.touchID fingerprint recognition success
- Other hardware is not supported.
Test recognition errors, identifies error is not empty
- LAError.systemCancel - which the authorization process is canceled system. Especially likely to occur in, App pushed back background.
- LAError.userCancel - authorization is canceled by the user.
- LAError.userFallback - the user selects a password, instead of or Touch ID Face ID.
Download
I realize with SwiftUI, Download:
https://github.com/zgpeace/iOSBiometrics.git
Run the code
- If the Touch ID or Face ID information is not enabled, print the following information.
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.”)
- FaceID simulator may be open or Touch ID. Simulator> Hardware> Face ID / Touch ID> Enrolled.
- Run the following error message:
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.
- Need to target> Info> Custom iOS Target Priorities> Add Key Value
key :
Privacy - Face ID Usage Description
value:
This app uses Face ID to confirm your identity
5. The first opened box will pop up to confirm
6. Click OK after the pop-up, Face ID verification interface
7. Click OK Simulator> Hardware> Face ID / ID Touch> Face Matching
8. The console Printing results are as follows:
Biometry is available on the device
msg > Authentication Successful
err > Optional("Device support Face ID")
reference
https://www.techotopia.com/index.php/Implementing_TouchID_Authentication_in_iOS_8_Apps