如何检测指纹传感器不可用的设备

时间:2017-02-03 07:37:18

标签: ios objective-c fingerprint touch-id

我在iOS应用中启用了touch id。但iPhone 5和5c的指纹传感器不可用。如何以编程方式检测没有指纹传感器的设备。我的应用程序是用objective-c编写的。

请帮帮我。 感谢

2 个答案:

答案 0 :(得分:1)

您应该使用Touch ID身份验证所需的LAContext框架。

LAErrorTouchIDNotAvailable显示哪些设备具有此功能。

代码段:

- (IBAction)shouldAuthenticate:(id)sender {
    LAContext *context = [[LAContext alloc] init];
    NSError *error = nil;

    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
    // Authentication here.
    } else {

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

    }
}    

或尝试此操作以获得BOOL返回:

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

答案 1 :(得分:0)

func checkIfTouchIDEnabled() {

var error:NSError?

// 2. Check if the device has a fingerprint sensor
// If not, show the user an alert view and bail out!
guard authenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) else {

    if let error = error as NSError? {

      if error.code == LAError.Code.touchIDNotAvailable.rawValue { /* Device does not support touch id*/

        print("Finger print sensors are not present in this device")

      } else if error.code == LAError.Code.touchIDNotEnrolled.rawValue {
        print("Finger print sensors are present in this device but no TouchID has been enrolled yet")
      } else {
        // Add more checks ...
      }
    }
  return
}
}