[Objc] Fingerprint authentication

A simple tutorial that how to use Apple fingerprint authentication in your iOS8apps on iPhone 5S and plus.

What you need:

  • iOS 8.0+
  • iPhone 5S or superior
  • LocalAuthentication.framework
  • XCode 6+

Intro

With iOS8, Apple introduced a new framework, called LocalAuthentication that is used to authenticate users on devices.

At this moment, the only one allowed authentication is

[code autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]LAPolicyDeviceOwnerAuthenticationWithBiometrics[/code]

as is shown in LocalAuthentication.framework/LAContext.h

[code autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]typedef NS_ENUM(NSInteger, LAPolicy)
{
/// Device owner was authenticated using a biometric method.
/// @discussion Biometrics (Touch ID) authentication is required. If Touch ID is not available or
///             not enabled, policy evaluation will fail.
///             Touch ID authentication dialog contains a cancel button and a fallback button with
///             default title "Enter Password" which can be customized using localizedFallbackTitle
///             property.
LAPolicyDeviceOwnerAuthenticationWithBiometrics NS_ENUM_AVAILABLE(NA, 8_0) = kLAPolicyDeviceOwnerAuthenticationWithBiometrics
}NS_ENUM_AVAILABLE(10_10, 8_0);[/code]

…surely in the future this ENUM grows up!

Implementation

It’s very easy to use in app the Biometrics authentication.
First of all, import the required framework:

Screenshot 2015-04-07 12.03.54

Next,
in your controller add the framework import:

[code autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]#import <Localauthentication/LocalAuthentication.h>[/code]

Bla bla bla, ok, but I want to copy some code!

Create the LAContext class

[code autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]LAContext *_context;
// […]
_context = [[LAContext alloc] init];[/code]

next, check if the device is allowed or support bio authentication:

[code autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]- (BOOL) canUseBiometrics
{
return [_context canEvaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil];
}[/code]

and call performAuthentication method when you want:

[code autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]- (void) performAuthentication
{
if ( ![self canUseBiometrics] ) return;

/*
Validate and verify a Touch ID fingerprint.
*/
[_context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:@"Authenticate for server login"
reply:^(BOOL success, NSError *authenticationError)
{
if ( success ) {
NSLog(@"User is authenticated successfully");
} else {
switch ( authenticationError.code ) {
case LAErrorAuthenticationFailed:
NSLog(@"Authentication Failed");
break;
case LAErrorUserCancel:
NSLog(@"User pressed Cancel button");
break;
case LAErrorUserFallback:
[self showPasswordAlert];
break;
}
}
}];
}[/code]

Well done!

If you want to use “passcode” as showed in popup,
2015-04-07 12.09.10

you should recognize the LAErrorUserFallback error, and add an UIAlertViewof type UIAlertViewStyleSecureTextInput and in his delegate, use the entered password as you like:

[code autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]- (void) showPasswordAlert
{
UIAlertView *passwordAlert = [[UIAlertView alloc]
initWithTitle:@"Demo"
message:@"Please type your password"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Ok", nil];

[passwordAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
[passwordAlert show];
}

– (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if ( buttonIndex == 1 )
{
NSLog(@"%@", [alertView textFieldAtIndex:0].text );
}
}

[/code]
that’s all.

Enjoy!

 

Alberto Pasca

Software engineer @ Pirelli & C. S.p.A. with a strong passion for mobile  development, security, and connected things.