If Device Supports Touch ID

2020-07-18 05:50发布

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

Thanks!

5条回答
Root(大扎)
2楼-- · 2020-07-18 06:16

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;
}
查看更多
Lonely孤独者°
3楼-- · 2020-07-18 06:17

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.

查看更多
Viruses.
4楼-- · 2020-07-18 06:32

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 ();
            }
        }
查看更多
姐就是有狂的资本
5楼-- · 2020-07-18 06:40

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.");
}
查看更多
姐就是有狂的资本
6楼-- · 2020-07-18 06:42

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

查看更多
登录 后发表回答