在Flutter中集成生物识别功能时,如何同时兼容指纹和面容ID验证?

在Flutter中集成生物识别功能时,如何同时兼容指纹和面容ID验证?我尝试使用local_auth插件,但在iOS设备上始终无法正确调起面容识别,调试时返回错误代码-1。具体需要配置哪些iOS的Info.plist权限?另外,Android和iOS的生物识别API调用方式是否有差异,如何处理平台间的兼容性问题?在用户禁用生物识别后,如何优雅地降级到密码验证流程?求最佳实践方案和代码示例。

3 回复

作为屌丝程序员,做生物识别集成可以用插件简化工作。对于指纹和面容ID,推荐使用local_auth插件。先添加依赖到pubspec.yaml:

dependencies:
  local_auth: ^2.1.0

然后按以下步骤实现:

  1. 检查设备支持:调用canCheckBiometrics确认设备是否支持生物识别。
  2. 列出可用的传感器:通过getAvailableBiometrics获取指纹或面容ID信息。
  3. 开始认证:调用authenticate方法弹出验证界面。

示例代码:

import 'package:local_auth/local_auth.dart';

final LocalAuthentication auth = LocalAuthentication();

void checkBiometric() async {
  bool canCheck = await auth.canCheckBiometrics;
  if (canCheck) {
    List<BiometricType> types = await auth.getAvailableBiometrics();
    print(types); // 打印支持的类型
    bool authenticated = await auth.authenticate(
      localizedReason: '请验证以继续',
      options: const AuthenticationOptions(stickyAuth: true),
    );
    if (authenticated) {
      print('验证成功');
    } else {
      print('验证失败');
    }
  }
}

注意不同平台配置,iOS需在info.plist添加隐私描述,Android需配置权限。这个方法简单高效,适合快速集成生物识别功能。

更多关于在Flutter中集成生物识别功能时,如何同时兼容指纹和面容ID验证?的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


作为屌丝程序员,实现指纹与面容ID验证可以用Flutter的local_auth插件。首先添加依赖:

dependencies:
  local_auth: ^2.0.0

然后请求权限并调用相关方法:

import 'package:local_auth/local_auth.dart';

final LocalAuthentication auth = LocalAuthentication();

Future<void> authenticateWithBiometrics() async {
  bool canCheckBiometrics = await auth.canCheckBiometrics;
  if (!canCheckBiometrics) return;

  List<BiometricType> availableBiometrics = await auth.getAvailableBiometrics();
  
  bool authenticated = false;
  try {
    authenticated = await auth.authenticate(
      localizedReason: '请验证身份',
      options: const AuthenticationOptions(stickyAuth: true),
    );
  } catch (e) {
    print(e);
  }

  if (authenticated) {
    print('验证成功');
  }
}

对于面容ID,BiometricType.face会包含在可用生物特征中。记住要处理异常情况并提供替代登录方式。

Flutter生物识别集成指南

在Flutter中集成生物识别认证(指纹和面容ID)可以使用local_auth插件,这是一个官方维护的插件,支持Android和iOS平台。

基本集成步骤

  1. 首先添加依赖到pubspec.yaml:
dependencies:
  local_auth: ^2.1.0
  1. 实现基本生物识别验证代码:
import 'package:flutter/material.dart';
import 'package:local_auth/local_auth.dart';

class BiometricAuthPage extends StatefulWidget {
  @override
  _BiometricAuthPageState createState() => _BiometricAuthPageState();
}

class _BiometricAuthPageState extends State<BiometricAuthPage> {
  final LocalAuthentication auth = LocalAuthentication();
  String _authorized = '未授权';

  Future<void> _authenticate() async {
    try {
      bool authenticated = await auth.authenticate(
        localizedReason: '请验证身份以继续',
        options: const AuthenticationOptions(
          biometricOnly: true, // 仅使用生物识别
          useErrorDialogs: true, // 显示系统错误对话框
          stickyAuth: true, // 保持认证状态
        ),
      );
      
      setState(() {
        _authorized = authenticated ? '授权成功' : '授权失败';
      });
    } catch (e) {
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('生物识别验证')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('状态: $_authorized'),
            ElevatedButton(
              child: Text('验证'),
              onPressed: _authenticate,
            ),
          ],
        ),
      ),
    );
  }
}

平台配置

Android:

  • AndroidManifest.xml中添加权限:
<uses-permission android:name="android.permission.USE_BIOMETRIC"/>
<uses-permission android:name="android.permission.USE_FINGERPRINT"/>

iOS:

  • Info.plist中添加:
<key>NSFaceIDUsageDescription</key>
<string>需要面容ID验证您的身份</string>

高级功能

  1. 检查可用生物识别类型:
List<BiometricType> availableBiometrics = await auth.getAvailableBiometrics();
  1. 检查设备是否支持生物识别:
bool canCheckBiometrics = await auth.canCheckBiometrics;

这个插件会自动处理平台差异,在Android上显示指纹验证,在支持Face ID的iOS设备上显示面容验证。

回到顶部