Flutter生物识别存储插件tbib_biometric_storage的使用
Flutter生物识别存储插件tbib_biometric_storage的使用
biometric_storage
插件是一个加密文件存储库,可以选择性地通过生物识别锁进行保护。适用于Android和iOS平台,但Android 5不支持生物识别功能,可以使用随机令牌进行加密。
此插件旨在以硬件加密的方式存储小数据,例如密码、密钥等,而不是大量数据。
安卓平台
- 使用 androidx 和 KeyStore。
- Android 5不支持生物识别功能,但可以使用随机令牌进行加密。
iOS平台
- 使用 LocalAuthentication 和 KeyChain。
开始使用
安装
Android
需求:
- Android: API Level >= 21 (
android/app/build.gradle
中minSdkVersion 21
) - 确保使用最新版本的 Kotlin:
android/build.gradle
:ext.kotlin_version = '1.7.10'
,使用 gradle 版本 7.3.0- classpath ‘com.android.tools.build:gradle:7.3.0’
- MainActivity 必须扩展
FlutterFragmentActivity
- 主活动的主题必须使用
Theme.AppCompat
主题(否则在 Android < 29 上会出现崩溃)AndroidManifest.xml
:<activity android:name=".MainActivity" android:launchMode="singleTop" android:theme="@style/LaunchTheme">
xml/styles.xml
:<style name="LaunchTheme" parent="Theme.AppCompat.NoActionBar"> <!-- 显示一个启动屏幕,自动移除当 Flutter 绘制其第一帧时 --> <item name="android:windowBackground">@drawable/launch_background</item> <item name="android:windowNoTitle">true</item> <item name="android:windowActionBar">false</item> <item name="android:windowFullscreen">true</item> <item name="android:windowContentOverlay">@null</item> </style>
iOS
- 包含
NSFaceIDUsageDescription
键在应用的 Info.plist 文件中 - 至少需要 iOS 9
已知问题:自 iOS 15 起,模拟器似乎不再支持本地身份验证
使用
你基本上只需要四个方法:
- 检查设备是否支持生物识别认证
final bool isBioSupported = await BiometricStorage().canAuth();
- 初始化生物识别认证
TBIBAuth().init(
android: const AndroidAuthMessages(
cancelButton: "Cancel",
goToSettingsButton: "Settings",
goToSettingsDescription: "Please set up your Touch IDs .",
biometricHint: "Touch sensors",
biometricNotRecognized: "Fingerprint not recognizeds.",
deviceCredentialsRequiredTitle: "Fingerprint requireds",
deviceCredentialsSetupDescription:
"Please set up your Touch ID or Face IDs.",
),
ios: const IOSAuthMessages(
cancelButton: "Cancel",
goToSettingsButton: "Settings",
goToSettingsDescription: "Please set up your Touch ID.",
lockOut: "Please reenable your Touch ID"));
- 在加密或解密数据之前需要请求授权
await BiometricStorage().auth("Login to save data");
- 写入数据
await BiometricStorage().write("Login", "data to save");
- 读取数据
await BiometricStorage().read("Login");
完整示例代码
import 'package:flutter/material.dart';
import 'package:tbib_biometric_storage/auth/android_auth_messages.dart';
import 'package:tbib_biometric_storage/auth/ios_auth_messages.dart';
import 'package:tbib_biometric_storage/biometric_storage.dart';
void main() {
TBIBAuth().init(
android: const AndroidAuthMessages(
cancelButton: "Cancel",
goToSettingsButton: "Settings",
goToSettingsDescription: "Please set up your Touch IDs .",
biometricHint: "Touch sensors",
biometricNotRecognized: "Fingerprint not recognizeds.",
deviceCredentialsRequiredTitle: "Fingerprint requireds",
deviceCredentialsSetupDescription:
"Please set up your Touch ID or Face IDs.",
),
ios: const IOSAuthMessages(
cancelButton: "Cancel",
goToSettingsButton: "Settings",
goToSettingsDescription: "Please set up your Touch ID.",
lockOut: "Please reenable your Touch ID"));
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _biometricStoragePlugin = BiometricStorage();
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
children: [
ElevatedButton(
onPressed: () async {
var auth =
await _biometricStoragePlugin.auth("Login to save data");
if (auth) {
await _biometricStoragePlugin.write(
"login", "user id 100s");
}
},
child: const Text("Save data demo")),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () async {
var auth =
await _biometricStoragePlugin.auth("Login to get data");
if (auth) {
var retrive = await _biometricStoragePlugin.read("login");
print("data retrive is $retrive");
}
},
child: const Text("Get data demo"))
],
)),
),
);
}
}
更多关于Flutter生物识别存储插件tbib_biometric_storage的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter生物识别存储插件tbib_biometric_storage的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
tbib_biometric_storage
是一个 Flutter 插件,它允许你在设备上安全地存储数据,并使用生物识别(如指纹、面部识别等)来保护这些数据。这个插件是基于 Android 的 BiometricPrompt
和 iOS 的 LocalAuthentication
API 实现的。
安装插件
首先,你需要在 pubspec.yaml
文件中添加 tbib_biometric_storage
插件的依赖:
dependencies:
flutter:
sdk: flutter
tbib_biometric_storage: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
使用插件
1. 初始化生物识别存储
在使用插件之前,你需要初始化 BiometricStorage
实例。你可以通过 BiometricStorage()
构造函数来创建实例。
import 'package:tbib_biometric_storage/tbib_biometric_storage.dart';
final biometricStorage = BiometricStorage();
2. 检查设备是否支持生物识别
在使用生物识别存储之前,你应该检查设备是否支持生物识别功能。
bool canAuthenticate = await biometricStorage.canAuthenticate();
if (canAuthenticate) {
print("Device supports biometric authentication.");
} else {
print("Device does not support biometric authentication.");
}
3. 存储数据
你可以使用 write
方法来存储数据。该方法会要求用户进行生物识别验证。
try {
await biometricStorage.write(
key: 'my_sensitive_data', // 存储的键名
value: 'This is a secret message.', // 存储的值
);
print("Data stored securely.");
} catch (e) {
print("Failed to store data: $e");
}
4. 读取数据
你可以使用 read
方法来读取之前存储的数据。该方法也会要求用户进行生物识别验证。
try {
String? value = await biometricStorage.read(key: 'my_sensitive_data');
if (value != null) {
print("Read data: $value");
} else {
print("No data found for the given key.");
}
} catch (e) {
print("Failed to read data: $e");
}
5. 删除数据
你可以使用 delete
方法来删除之前存储的数据。
try {
await biometricStorage.delete(key: 'my_sensitive_data');
print("Data deleted successfully.");
} catch (e) {
print("Failed to delete data: $e");
}
注意事项
-
权限: 在 Android 上,你可能需要在
AndroidManifest.xml
中添加以下权限:<uses-permission android:name="android.permission.USE_BIOMETRIC"/> <uses-permission android:name="android.permission.USE_FINGERPRINT"/>
-
iOS 配置: 在 iOS 上,你需要在
Info.plist
中添加以下条目来描述你使用生物识别的原因:<key>NSFaceIDUsageDescription</key> <string>We use Face ID to secure your data.</string>