Use Apple FaceID or TouchID authentication in your app using BiometricAuthentication. It's very simple and easy to use that handles Touch ID and Face ID authentication based on the device.
Note: - Face ID authentication requires user's persmission to be add in info.plist.
<key>NSFaceIDUsageDescription</key>
<string>This app requires Face ID permission to authenticate using Face recognition.</string>
// set this before calling authenticateWithBioMetrics method (optional) BioMetricAuthenticator.shared.allowableReuseDuration = 60
### Version 2.1
- Check if **TouchID** or **FaceID** authentication is available for iOS device.
![Alt text](https://raw.githubusercontent.com/rushisangani/BiometricAuthentication/master/Images/image1.png "Authenticate")
![Alt text](https://raw.githubusercontent.com/rushisangani/BiometricAuthentication/master/Images/image2.png "Fallback title")
![Alt text](https://raw.githubusercontent.com/rushisangani/BiometricAuthentication/master/Images/image3.png "Locked out")
## Features
- Works with Apple Face ID (iPhone X, Xs, XR, XsMax) and other Touch ID having devices.
- Predefined error handling when recognition fails.
- Automatic authentication with device passcode on multiple failed attempts.
## Requirements
- iOS 12.0+
- Xcode 10+
- Swift 3.0+
## Installation
### CocoaPods
```ruby
pod 'BiometricAuthentication'
github "rushisangani/BiometricAuthentication"
BioMetricAuthenticator.authenticateWithBioMetrics(reason: "") { (result) in
switch result {
case .success( _):
print("Authentication Successful")
case .failure(let error):
print("Authentication Failed")
}
}
if BioMetricAuthenticator.canAuthenticate() {
BioMetricAuthenticator.authenticateWithBioMetrics(reason: "") { (result) in
// check result -> success or failure
}
}
if BioMetricAuthenticator.shared.faceIDAvailable() {
// device supports face id recognition.
}
if BioMetricAuthenticator.shared.touchIDAvailable() {
// device supports touch id authentication
}
BioMetricAuthenticator.authenticateWithBioMetrics(reason: "Biometric Authentication", fallbackTitle: "Enter Credentials") { (result) in
switch result {
case .success( _):
// proceed further
case .failure(let error):
switch error {
case .fallback:
print("Authentication Failed")
// show alternatives on fallback button clicked
// for ex. - enter username/email and password
default:
break
}
}
}
BioMetricAuthenticator.authenticateWithPasscode(reason: message) { (result) in
switch result {
case .success( _):
// passcode authentication success
case .failure(let error):
print(error.message())
}
}
BioMetricAuthenticator.authenticateWithBioMetrics(reason: "") { [weak self] (result) in
switch result {
case .success( _):
// authentication successful
self?.showLoginSucessAlert()
case .failure(let error):
switch error {
// device does not support biometric (face id or touch id) authentication
case .biometryNotAvailable:
self?.showErrorAlert(message: error.message())
// No biometry enrolled in this device, ask user to register fingerprint or face
case .biometryNotEnrolled:
self?.showGotoSettingsAlert(message: error.message())
// show alternatives on fallback button clicked
case .fallback:
self?.txtUsername.becomeFirstResponder() // enter username password manually
// Biometry is locked out now, because there were too many failed attempts.
// Need to enter device passcode to unlock.
case .biometryLockedout:
self?.showPasscodeAuthentication(message: error.message())
// do nothing on canceled by system or user
case .canceledBySystem, .canceledByUser:
break
// show error for any other reason
default:
self?.showErrorAlert(message: error.message())
}
}
}
See Example for more details.
BiometricAuthentication is released under the MIT license. See LICENSE for details.