在移动应用开发中,用户隐私和安全至关重要。你是否想过为APP添加类似手机解锁的本地身份验证功能?今天介绍的local_auth插件,正是Flutter开发者实现指纹、人脸识别的终极武器!无需复杂代码,轻松打造安全又炫酷的认证体验
功能亮点
- 多平台支持:兼容iOS(Face ID/Touch ID)和Android(指纹/人脸)
- 一键验证:调用系统原生界面,用户体验无缝衔接
- 灵活配置:支持生物识别+密码双验证模式
- 异常处理:智能识别硬件锁死、权限异常等场景
安装方式
csharp
flutter pub add local_auth
详细配置和示例
iOS和安卓平台的配置
iOS需要再info.plist中配置face ID的权限
vbnet
<key>NSFaceIDUsageDescription</key>
<string>Why is my app authenticating using face id?</string>
安卓配置如下
1、修改安卓的原生MainActivity的父类
kotlin
import io.flutter.embedding.android.FlutterActivity
class MainActivity : FlutterActivity()
// 修改为:
import io.flutter.embedding.android.FlutterFragmentActivity
class MainActivity : FlutterFragmentActivity ()
2、在AndroidManifest.xml添加权限
ini
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<uses-permission android:name="android.permission.USE_BIOMETRIC"/>
<manifest>
硬件能力检测
ini
final auth = LocalAuthentication();
// 检测设备是否支持
bool isSupported = await auth.isDeviceSupported();
// 查看已注册的生物识别类型
List<BiometricType> types = await auth.getAvailableBiometrics();
// 可能返回:fingerprint(指纹)、face(人脸)、strong(高安全级别)
完整代码示例
dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:local_auth/local_auth.dart';
class TestPage extends StatefulWidget {
const TestPage({super.key});
@override
State<TestPage> createState() => _TestPageState();
}
class _TestPageState extends State<TestPage> {
final LocalAuthentication auth = LocalAuthentication();
Future<void> _checkBiometrics() async {
// canCheckBiometrics 仅指示硬件是否支持,并不表示设备是否已注册任何生物识别信息,有可能未录入指纹或者人脸
final bool canAuthenticateWithBiometrics = await auth.canCheckBiometrics;
final bool isDeviceSupported = await auth.isDeviceSupported();
debugPrint(
"canAuthenticateWithBiometrics = $canAuthenticateWithBiometrics isDeviceSupported = $isDeviceSupported",
);
}
Future<void> _getAvailableBiometricsData() async {
final List<BiometricType> availableBiometrics = await auth
.getAvailableBiometrics();
if (availableBiometrics.isEmpty) {
debugPrint("设备未注册任何生物识别信息");
return;
}
for (BiometricType item in availableBiometrics) {
debugPrint("type = ${item.name}");
}
// if (availableBiometrics.contains(BiometricType.strong) ||
// availableBiometrics.contains(BiometricType.face)) {
// }
}
Future<void> authenticateTest() async {
try {
final bool didAuthenticate = await auth.authenticate(
localizedReason: 'Please authenticate to show account balance',
biometricOnly: true, // 强制使用生物识别认证
persistAcrossBackgrounding: true, // 应用未验证就退到后台,再次在前台运行时,重试身份验证
);
if (didAuthenticate) {
debugPrint("success");
} else {
debugPrint("fail");
}
} on LocalAuthException catch (e) {
// 异常处理 temporaryLockout biometricLockout 指纹或face ID被锁定
debugPrint("LocalAuthException ${e.toString()}");
} on PlatformException catch (e) {
debugPrint("PlatformException ${e.message}");
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('local_auth')),
body: Column(
children: [
ElevatedButton(
onPressed: () {
_checkBiometrics();
},
child: const Text('检查此设备是否支持本地身份验证'),
),
ElevatedButton(
onPressed: () {
_getAvailableBiometricsData();
},
child: const Text('获取注册生物识别信息的列表'),
),
ElevatedButton(
onPressed: () {
authenticateTest();
},
child: const Text('Check biometrics'),
),
],
),
),
);
}
}
以上就是该库在移动端的使用情况了。