Integarting iOS Touch ID (Obj C)

In September 2013, apple unveiled the most innovative feature of iPhone 5s, the thin metal band on home button, a fingerprint scanner called Touch ID.

In this tutorial we have used Xcode 6, you will also need a device with touch ID for testing.

What id Touch ID Exactly?

appTuition-TouchId

Touch ID is fingerprint identification sensor that is used to unlock your iPhone and also to grant access to much more things.  As your fingerprint is the perfect biometric identity unlocking becomes much easier, faster and secure comparing to pin or passcode.

Local Authentication framework, is the heart of Touch ID functionality. It handles all the verification/ authentication. More importantly, framework reveals no information about use and no data transfer takes place from/to the device. We can only use this framework to check if a user is allowed to access application.

LAContext class, we will create the instance of this class to check if the user trying to authenticate is device’s owner.

 

Lets create a sample app to demonstrate the usability of Local Authentication framework and Touch ID.

 

Setting Up New Project

Step 1:

Run Xcode, select Create a new Xcode project and then choose Single view application.

 

Step 2:

Now, enter the name of the app, mine is TouchIDAuth, you can enter whatever you like.

Enter organization’s name and organization identifier.  Choose Objective-C as language and iPhone from device list. As of now uncheck core Data(gonna have separate tutorial for it) and click Next. Choose the location of the source when prompt.

 

TouchIdIntegration-Objc_Image1

Step 3:

Now, drag and drop the button from panel to the main.storyboard‘s View controller.

TouchIdIntegration-Objc_Image1 

 

Step 4:

Connect the button to ViewController.m class.

TouchIdIntegration-Objc_Image2

 

And name it as btnAuthenticatePressed (you can name it anything you want – the choice is yours 😉 ).

TouchIdIntegration-Objc_Image3

 

 

Coding Part

Step 1:

Open ViewController.m class and on the add LocalAuthentication framework using:

1
#import <LocalAuthentication/LocalAuthentication.h>;

Step 2:

Create object of LAContext class and using LAPolicyDeviceOwnerAuthenticationWithBiometrics check if device compatibility. If there is issue show suitable error message other wise if device is compatible, check if user is device owner or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
- (IBAction)btnAuthenticatePressed:(id)sender {
LAContext *context = [[LAContext alloc] init];

NSError *error = nil;

if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:@"Authenticating device owner."
reply:^(BOOL success, NSError *error) {

if (error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error:" message:@"Sorry, unable to authenticate." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
NSLog(@"Error: %@",error.description);
return;
}

if (success) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Successfully authenticated user."delegate:nil cancelButtonTitle:@"Ok"otherButtonTitles:nil];
[alert show];
}

else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error:"message:@"Something went wrong." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
NSLog(@"Error: %@",error.description);
}
}];
} else {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"message:@"Ddevice is not compactible." delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil];
[alert show];
}

}

Done, Now Run and check you app.