【问题标题】:Can't check Face ID when user denied to use用户拒绝使用时无法检查人脸 ID
【发布时间】:2018-01-22 13:29:15
【问题描述】:

如果设备支持Face IDTouch ID,我想对用户采取不同的操作。

当使用 Face ID 时,iOS 请求使用权限。 (与触控 ID 不同)。

如果用户拒绝许可,context.biometryType 返回 LABiometryTypeNone。

是否有办法检查设备支持的Touch IDFace ID

LAContext *context = [[LAContext alloc] init];

NSError *error;

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

}

if (@available(iOS 11.0, *)) {
    if (context.biometryType == LABiometryTypeFaceID) {
        // support FaceID 
    }
}

// support TouchID

控制台输出

(lldb) po error
Error Domain=com.apple.LocalAuthentication Code=-6 "User has denied the use of biometry for this app." UserInfo={NSLocalizedDescription=User has denied the use of biometry for this app.}

(lldb) po context.biometryType
LABiometryTypeNone

注意:我不想使用密码验证。我只需要知道 设备支持 Touch ID 或 Face ID

【问题讨论】:

  • 您找到问题的答案了吗?我有同样的问题。

标签: ios touch-id face-id


【解决方案1】:

没有。如果用户使用NSFaceIDUsageDescription 拒绝隐私权限提示,那么一旦对它们调用canEvalutePolicy:error:,所有LAContext 的未来实例将具有.nonebiometryType 属性。

【讨论】:

    【解决方案2】:

    为时已晚,但我希望这可以帮助遇到同样问题的人。

    LAContext().canEvaluatePolicy(.deviceOwnerAuthentication,错误:nil) LAContext().canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics,错误:nil)

    deviceOwnerAuthenticationdeviceOwnerAuthenticationWithBiometrics 之间的区别第一个会告诉您设备是否具有身份验证方法.. 第二个具有相同的行为但只有用户接受才能工作权限。

    enum BiometryResult: Int {
    case faceID
    case touchID
    case notExist
    }
    class func biometryType() -> BiometryResult {
        let context = LAContext()
        if (context.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil)) {
            if (context.biometryType == LABiometryType.faceID) {
                return .faceID
            } else if (context.biometryType == LABiometryType.touchID) {
                return .touchID
            } else {
                return .notExist
            }
        }
        return .notExist
    }
    

    【讨论】:

      【解决方案3】:

      使用LAContext 的属性biometryType 来检查和评估可用的生物识别策略。 (对于密码认证,当生物识别失败时,使用:LAPolicyDeviceOwnerAuthentication

      试试这个看看:

      LAContext *laContext = [[LAContext alloc] init];
      
      NSError *error;
      
      
      // For a passcode authentication , when biometric fails, use: LAPolicyDeviceOwnerAuthentication
      //if ([laContext canEvaluatePolicy: LAPolicyDeviceOwnerAuthentication error:&error]) {
      if ([laContext canEvaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {    
          if (error != NULL) {
              // handle error
          } else {
      
              if (@available(iOS 11, *)) {
                  if (laContext.biometryType == LABiometryTypeFaceID) {
                      //localizedReason = "Unlock using Face ID"
                      NSLog(@"FaceId support");
                  } else if (laContext.biometryType == LABiometryTypeTouchID) {
                      //localizedReason = "Unlock using Touch ID"
                      NSLog(@"TouchId support");
                  } else {
                      //localizedReason = "Unlock using Application Passcode"
                      NSLog(@"No biometric support or Denied biometric support");
                  }
              } else {
                  // Fallback on earlier versions
              }
      
      
              [laContext evaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"Test Reason" reply:^(BOOL success, NSError * _Nullable error) {
      
                  if (error != NULL) {
                      // handle error
                  } else if (success) {
                      // handle success response
                  } else {
                      // handle false response
                  }
              }];
          }
      }
      

      【讨论】:

      • 正如我提到的,如果用户拒绝使用 Face ID,biometryType 将返回 LABiometryTypeNone。所以我不能采取适当的行动。可以看到控制台输出。
      • 是的,当然,当用户拒绝时,它会自动显示Passcode屏幕。
      • 对于密码验证,如果生物识别失败,请使用:LAPolicyDeviceOwnerAuthentication
      • 是的,我知道,这对我的问题没有帮助。我只需要知道设备是否支持 Touch ID 或 Face ID。
      • LAPolicyDeviceOwnerAuthentication 为您提供所有结果,只需尝试使用我的代码即可。如果使用已拒绝许可,则 if 将陷入错误块。否则,它将为您提供适用于 iOS 11 的可用生物识别类型(face-id 或 touch-id)。注意:对于 iOS 10 及更低版本,有/不支持 face-id。
      猜你喜欢
      • 2020-07-20
      • 2017-07-30
      • 2018-06-19
      • 2012-11-29
      • 2016-07-26
      • 1970-01-01
      • 2017-06-01
      • 1970-01-01
      • 2021-08-27
      相关资源
      最近更新 更多