Friday, 15 July 2016

Simple TouchId Integration Code in iOS

The Local Authentication framework provides facilities for requesting authentication from users with specified security policies. 
Steps:
1. Create a Xcode Project Lets Say(TouchIdIntegrationDemo).
2. Now add LocalAuthentication_Framework.

3. Now go to StoryBoard and take a Button and make Proper Connection
4. Now go to Your ViewController.m and add below code.

#import "ViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)identifyMeButtonAction:(id)sender {
    
    LAContext *laContext = [[LAContext alloc] init];
    
    NSError *error = nil;
    if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
        [laContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
        localizedReason:@"Are you really the device owner?"
          reply:^(BOOL success, NSError *error) {
              
              if (error) {
                  UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Error" message:@"Some Problem Occured" preferredStyle:UIAlertControllerStyleAlert];
                   UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
                  [controller addAction:defaultAction];
                  [self presentViewController:controller animated:YES completion:nil];
                  return;
              }
              
              if (success) {
                UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Success" message:@"Hey its Your Device" preferredStyle:UIAlertControllerStyleAlert];
                  UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
                  [controller addAction:defaultAction];
                  [self presentViewController:controller animated:YES completion:nil];
                 
                  
              } else {
                  UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Error" message:@"Hey its not Your Phone" preferredStyle:UIAlertControllerStyleAlert];
                  UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
                  [controller addAction:defaultAction];
                  [self presentViewController:controller animated:YES completion:nil];
              }
              
          }];

        } else {
    
            UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Error" message:@"Touch Id Not Supported" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
            [controller addAction:defaultAction];
            [self presentViewController:controller animated:YES completion:nil];
        
    }

    
    
}

@end

5. Run the Code and see the output. To download the whole Source Code Click Here
  Enjoy Coding :)
 

    No comments:

    Post a Comment

    Ads Inside Post

    Blogger Widget