Skip to content

Instantly share code, notes, and snippets.

@hansemannn
Created March 22, 2016 10:37
Show Gist options
  • Select an option

  • Save hansemannn/d28d28964334fccc280f to your computer and use it in GitHub Desktop.

Select an option

Save hansemannn/d28d28964334fccc280f to your computer and use it in GitHub Desktop.
//
// TiTouchId.h
// TiTouchId
//
// Created by Hans Knöchel on 22.03.16.
// Copyright © 2016 Hans Knoechel. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <LocalAuthentication/LocalAuthentication.h>
@interface TiTouchId : NSObject {
LAContext *context;
}
- (BOOL)isSupported;
- (void)authenticateWithReason:(NSString*)reason andCallback:(void(^)(NSDictionary *attrs))callback;
@end
#import "TiTouchId.h"
#import <UIKit/UIKit.h>
@implementation TiTouchId
-(instancetype)init
{
if (self = [super init]) {
context = [LAContext new];
}
return self;
}
-(BOOL)isSupported
{
NSError *error;
BOOL currentOSSupported = [[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] != NSOrderedAscending;
BOOL touchIDSupported = [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];
return currentOSSupported && touchIDSupported && !error;
}
-(void)authenticateWithReason:(NSString*)reason andCallback:(void(^)(NSDictionary *attrs))callback
{
if (!reason || !callback) {
NSLog(@"Ti.TouchID: Please provide a valid 'reason' and 'callback' parameter.");
return;
}
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:reason reply:^(BOOL success, NSError *error) {
NSMutableDictionary *attrs = [@{@"success":[NSNumber numberWithBool:success]} mutableCopy];
if (error) {
[attrs setValue:[error localizedDescription] forKey:@"error"];
[attrs setValue:[NSNumber numberWithInteger:[error code]] forKey:@"code"];
}
callback(attrs);
}];
}
@end
#import "TiTouchId.h"
TiTouchId *touchId = [TiTouchId new];
if ([touchId isSupported]) {
[touchId authenticateWithReason:@"Please verify to continue" andCallback:^(NSDictionary *attrs) {
BOOL success = [attrs valueForKey:@"success"];
NSError *error = [attrs valueForKey:@"error"];
NSLog(@"Success - %d", success);
if (error) {
NSLog(@"Error - %@", [error localizedDescription]);
NSLog(@"Code - %lu", [error code]);
}
}];
} else {
NSLog(@"Touch ID is not supported on this device");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment