iOS Biometric Local Authentication

@import LocalAuthentication;
Case 1 : Device is enabled with Biometric Sensor or not.
Case 2 : At Least one Fingerprint is enrolled in device or not to check Biometric sensor availability.
  1. deviceOwnerAuthenticationWithBiometrics: It puts restriction to use only biometric authentication to authenticate the device owner.
LAContext *context = [[LAContext alloc] init];
NSError *authError = nil;
Test if fingerprint authentication is available on
the device and a fingerprint has been enrolled.
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:HAS_DEVICE_ENROLLED_TOUCH_ID];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:HAS_DEVICE_BIOMETRY_SENSOR];
Biometric policy is successfully evaluated
device is enabled with Biometric sensor and
one fingerprint is enrolled in device.
}
else {
Could not evaluate policy;
look at authError and present an appropriate message to user
if(authError){
if (@available(iOS 11.0, *)) {
if(authError.code == LAErrorBiometryNotAvailable){
Device is not enable with Biometry sensor
on iOS version 11 onwards
}
else if(authError.code == LAErrorBiometryNotEnrolled){
Device is enable with Biometry sensor but
touchID is not enrolled in Device
}
}
else {
Fallback approach on earlier versions
if (authError.code == LAErrorTouchIDNotAvailable){
Device is not enable with Biometry sensor
on iOS version is below 11
}
else if(authError.code == LAErrorTouchIDNotEnrolled){
Device is enable with Biometry sensor but
touchID is not enrolled in Device
}
}
}
}
}
Before iOS 11 it was TouchIDLockout, touchIDNotAvailable, and touchIDNotEnrolled 
respectively.
- (void)viewDidLoad
{
hasBiometrySensor = [[NSUserDefaults standardUserDefaults] boolForKey:HAS_DEVICE_BIOMETRY_SENSOR];
if(SHOW_FINGERPRINT_LOGIN && hasBiometrySensor){
[self.biometryView setHidden:NO];
}
else{
[self.biometryView setHidden:YES];
}
}
-(void)setUPTouchIDInDevice{      UIAlertActionButton *button = [[UIAlertActionButton alloc] initWithTitle:@"OK"
style:UIAlertActionStyleDefault
andActionHandler:^{
NSURL *url = [NSURL URLWithString:@"App-Prefs:root=TOUCHID_PASSCODE"];
if([[UIApplication sharedApplication] canOpenURL:url]){
[[UIApplication sharedApplication] openURL:url];
NSLog(@"go to device settings to enroll fingerprint");
}
}];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Touch ID Setup"
message:@"Please setup Touch ID in your device"
actionButtons:@[button]
presentViewController:self
animation:YES
andCompletionHandler:nil];
[alert showOPActionAlert];
}
-(void)enrolledTouchIDInAppliction{

LAContext *context = [[LAContext alloc] init];
NSError *authError = nil;

Test if fingerprint authentication is available
on the device and a fingerprint has been enrolled.

NSString *myLocalizedReasonString = @"Please authenticate using your fingerprint.";

if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {

[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics

localizedReason:myLocalizedReasonString

reply:^(BOOL success, NSError *error) {
if (success) {
NSLog(@"User authenticated successfully, take appropriate action");
}
else {
NSLog(@"User did not authenticate successfully, look at error and take appropriate action");
if(error.code == -1){
NSLog(@"Application retry limit exceeded");
}
else if(error.code == LAErrorUserCancel){
NSLog(@"user has tapped the home button and authentication is canced by user");
}
if (@available(iOS 11.0, *)) {
if(error.code == LAErrorBiometryLockout){

NSLog(@"Authentication was not successful,
because there were too many failed biometry attempts(5 consequitive attempts)and biometry is now locked.Passcode is required to unlock biometry");
}
else if(error.code == LAErrorSystemCancel){
NSLog(@"Authentication was canceled by system (e.g. another application went to foreground).");
}
else if(error.code == LAErrorSystemCancel){
NSLog(@"Authentication was canceled by system (e.g. another application went to foreground).");
}
else {}
}
else {
// Fallback approach on earlier versions
if (error.code == LAErrorTouchIDLockout){

NSLog(@"Authentication was not successful,because there were too many failed biometry attempts and biometry is now locked.Passcode is required to unlock biometry");
}
else {}
}
}
}];
}
else {

if (authError.code) {

NSLog(@"There is no need to handle evaluate policy auth error as user is already handled the policy evaluated error in app delegate if user is not handling the policy evaluated error in app delegate then handle the auth error here.");
}
}
}

If you liked this article, please follioowing me on Medium. When I have something new and awesome to share. It’s the best way to find out when I write more articles like this.

--

--

--

iOS Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Succeed at Your Tech Internship This Summer

CQRS — Command Query Responsibility Segregation

Printing structs in Go

Jarbas: 1 | Vulnhub Walkthrough

Python: Continue Statement Example

Static and Dynamic Memory Allocation

Coronavirus update — A note from our CEO [UPDATED MARCH 13]

State Management for User Interfaces

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shubham Jain

Shubham Jain

iOS Developer

More from Medium

Every iOS developer should know these Xcode tricks

Sending Message Error To Webhook Slack

Best way to use Font Name using enum in iOS-Swift

Week #4 — Lagbaja and My First iOS Job Interview