You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用local_auth 1.1.11时双生物特征共存导致验证弹窗异常

解决local_auth 1.1.11多生物识别同时显示的问题

问题根源

你当前使用的local_auth 1.1.11版本中,authenticate方法的biometricOnly参数仅用于限制验证方式为生物识别(排除密码等),无法指定单一生物特征类型。当设备同时录入指纹和Face ID时,系统默认会启用所有可用的生物识别方式,导致验证弹窗同时显示两种选项。

解决方案

由于该版本本身不支持直接指定单一生物识别类型,需要通过平台特定的原生代码扩展来实现:

1. Android端实现

通过自定义平台通道,调用原生Android代码指定仅使用目标生物识别类型:

  • 原生代码中,构建BiometricPrompt.PromptInfo时,设置setAllowedAuthenticators为对应类型:
    • 指纹:BiometricManager.Authenticators.BIOMETRIC_FINGERPRINT
    • 面部识别:BiometricManager.Authenticators.BIOMETRIC_FACE
  • 调用BiometricPrompt.authenticate()触发验证,此时系统只会启动指定的生物识别传感器。

2. iOS端实现

同样通过平台通道调用原生iOS代码,限制LAContext的生物识别类型:

  • 创建LAContext实例后,指定biometryType为对应类型:
    • Touch ID:LABiometryType.touchID
    • Face ID:LABiometryType.faceID
  • 调用evaluatePolicy(_:localizedReason:reply:)方法完成验证,系统将仅触发指定的生物识别方式。

调整后的Dart层代码示例

假设已完成原生端平台通道的实现,Dart代码可修改为:

static Future<bool> authenticate() async {
  final isAvailable = await hasBiometrics();
  if (!isAvailable) return false;

  List<BiometricType> availableBiometrics = await _auth.getAvailableBiometrics();
  if (availableBiometrics.contains(BiometricType.fingerprint)) {
    try {
      // 调用自定义平台通道的指纹验证方法
      return await MethodChannel('your_channel_name/biometric').invokeMethod('authenticateFingerprint');
    } on PlatformException catch (e) {
      debugPrint(e.toString());
      myToastError('请先添加指纹');
      return false;
    }
  }
  return Future.value(true);
}

static Future<bool> authenticateFaceId() async {
  final isAvailable = await hasBiometrics();
  if (!isAvailable) return false;

  List<BiometricType> availableBiometrics = await _auth.getAvailableBiometrics();
  if (availableBiometrics.contains(BiometricType.faceId)) {
    try {
      // 调用自定义平台通道的Face ID验证方法
      return await MethodChannel('your_channel_name/biometric').invokeMethod('authenticateFaceId');
    } on PlatformException catch (e) {
      debugPrint(e.toString());
      myToastError('请先添加Face ID');
      return false;
    }
  } else {
    return Future.value(false);
  }
}

替代方案(不推荐,若版本兼容可尝试)

如果可以升级local_auth版本,后续部分版本新增了指定生物识别类型的参数(通过authenticators参数),但由于你依赖BiometricType.fingerprint和faceId枚举值,此方案可能需要调整代码适配新的枚举类型。

内容的提问来源于stack exchange,提问作者Bunny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 07:45:25