问题
Wondering how I can determine if the device the user has supports the Touch ID API? Hopefully have this as a boolean value.
Thanks!
回答1:
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
回答2:
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.
回答3:
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 ();
}
}
回答4:
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;
}
回答5:
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.");
}
来源:https://stackoverflow.com/questions/27123941/if-device-supports-touch-id