4

Wondering how I can determine if the device the user has supports the Touch ID API? Hopefully have this as a boolean value.

Thanks!

Harry
  • 3,301
  • 7
  • 27
  • 33

5 Answers5

5

try this:

- (BOOL)canAuthenticateByTouchId {
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
        return [[[LAContext alloc] init] canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil];
    }
    return NO;
}

or like @rckoenes suggest:

- (BOOL)canAuthenticateByTouchId {
    if ([LAContext class]) {
        return [[[LAContext alloc] init] canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil];
    }
    return NO;
}

UPDATE

I forgot, check this: How can we programmatically detect which iOS version is device running on? to define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO

Community
  • 1
  • 1
Mateusz
  • 1,222
  • 11
  • 22
  • 1
    Better use `if ([LAContext class]) {` it stead of checking the the system version. Since you are interested if the class is available and not really in the system version. You should refrain from checking system version, either check foundation version or just if the class or methods is available. – rckoenes Nov 25 '14 at 10:21
  • 1
    Just to add: This does not detect whether the device has TouchID feature or not. It only check if the TouchID is enabled or not in the device Settings. The answer from Siti Kamaludin below is better. – GeneCode Jul 05 '17 at 03:20
5

You should consider LAContext framework that is required to Touch ID authentication.

And parameter LAErrorTouchIDNotAvailable will show is devise support this functionality.

Code snippet :

- (IBAction)authenticateButtonTapped:(id)sender {
    LAContext *context = [[LAContext alloc] init];

    NSError *error = nil;

    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
        // Authenticate User

    } else {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Your device cannot authenticate using TouchID."
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
        [alert show];

    }
}

Nice tutorial to learn this feature is here.

Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100
4

You can check the error using CanEvaluatePolicy. If the Error Code is -6, it means no physical Touch Id on that device. You can tell from the Error Description, it says

Biometry is not available on this device.

Below is the code if you're using C# Xamarin:

var context = new LAContext();
        NSError AuthError;
        if (!context.CanEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, out AuthError))
        {
            if ( AuthError != null && AuthError.Code == -6 )
            {
                var alert = new UIAlertView ("Error", "TouchID not available", null, "BOOO!", null);
                alert.Show ();
            }
        }
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
0

This function will help with that -

-(BOOL)doesThisDeviceSupportTouchIdForLocalAuthentication{

    //Checking for 64 bit (armv7s) architecture before including the LAContext as it would give error otherwise.
    #if TARGET_CPU_ARM64
    LAContext *context = [[LAContext alloc] init];

    NSError *error = nil;

    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]){
        return YES;
    }
    return NO;
    #endif

    return NO;
}
Rahul Mane
  • 1,005
  • 18
  • 33
mmmanishs
  • 601
  • 6
  • 10
0

Objective c

@import LocalAuthentication;
// Get the local authentication context:
LAContext *context = [[LAContext alloc] init];
// Test if fingerprint authentication is available on the device and a fingerprint has been enrolled.
if ([context canEvaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil])
{
    NSLog(@"Fingerprint authentication available.");
}
AlexanderZ
  • 2,128
  • 3
  • 20
  • 24
TwoPlay
  • 1
  • 2
  • Welcome to Stack Overflow! [How to Answer](http://stackoverflow.com/help/how-to-answer) can help you write answers that will be accepted and upvoted. In your case, code should be put in a code block. – zhon Sep 04 '16 at 19:45