iOS Touch ID Authentication API Tutorial


In iOS Touch ID Authentication tutorial, I have explained how to use Touch ID authentication API in iPhone / iPad apps. Touch ID API is introduced in iOS 8.

Touch ID Authentication window provides three options to user

a). Authenticate user with fingerprint

b). Enter Password. It is a fallback mechanism.

If the user is not willing to authenticate using Touch ID, he can click on "Enter Password". So App should have a fallback mechanism to authenticate user.

c). Cancel

User can click on "Cancel" to stop authentication.


Follow the steps to add Touch ID Authentication to your App:

Step 1). To use Touch ID API, you need to import Local Authentication Framework



Step 2) To use API, we need to create Local Authentication Context.

LAContext *myContext = [[LAContext alloc] init];


Step 3). Check whether Touch ID is configured and can be used for authentication.

- (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error;


Step 4). To show Touch ID Authentication use the following method.

- (void)evaluatePolicy:(LAPolicy)policy 
					localizedReason:(NSString *)localizedReason 
					reply:(void(^)(BOOL success, NSError *error))reply;


localizedReason ->You need to provide localizedReason, to show it to user.

reply -> block is called when authentication is successful. If the success parameter is YES,then authentication is successful otherwise authentication is failed.
You can get the error code using error.code. Below is the list of errors

typedef NS_ENUM(NSInteger, LAError)
 /// Authentication was not successful, because user failed to provide valid credentials.
 LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,

 /// Authentication was canceled by user (e.g. tapped Cancel button).
 LAErrorUserCancel = kLAErrorUserCancel,

 /// Authentication was canceled, because the user tapped the fallback button (Enter Password).
 LAErrorUserFallback = kLAErrorUserFallback,

 /// Authentication was canceled by system (e.g. another application went to foreground).
 LAErrorSystemCancel = kLAErrorSystemCancel,

 /// Authentication could not start, because passcode is not set on the device.
 LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet,

 /// Authentication could not start, because Touch ID is not available on the device.
 LAErrorTouchIDNotAvailable = kLAErrorTouchIDNotAvailable,

 /// Authentication could not start, because Touch ID has no enrolled fingers.
 LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled,
} NS_ENUM_AVAILABLE(10_10, 8_0);


iOS Touch ID Authentication API Example:

 LAContext *myContext = [[LAContext alloc] init];
 NSError *authError = nil;
 NSString *myLocalizedReasonString = @"Authenticate using your finger";
 if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {

 [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
 reply:^(BOOL succes, NSError *error) {

 if (succes) {

 NSLog(@"User is authenticated successfully");
 } else {

 switch (error.code) {
 case LAErrorAuthenticationFailed:
 NSLog(@"Authentication Failed");

 case LAErrorUserCancel:
 NSLog(@"User pressed Cancel button");

 case LAErrorUserFallback:
 NSLog(@"User pressed "Enter Password"");

 NSLog(@"Touch ID is not configured");

 NSLog(@"Authentication Fails");
 } else {
 NSLog(@"Can not evaluate Touch ID");