生物识别
在 Android 和 iOS 设备上向用户弹出生物识别认证。
This plugin requires a Rust version of at least 1.77.2
| Platform | Level | Notes |
|---|---|---|
| windows | | |
| linux | | |
| macos | | |
| android | ||
| ios |
安装生物识别插件开始使用。
使用你项目的包管理器添加依赖:
npm run tauri add biometricyarn run tauri add biometricpnpm tauri add biometricdeno task tauri add biometricbun tauri add biometriccargo tauri add biometric-
在
src-tauri文件夹中运行以下命令,将插件添加到Cargo.toml项目的依赖项中:cargo add tauri-plugin-biometric --target 'cfg(any(target_os = "android", target_os = "ios"))' -
修改
lib.rs以初始化插件:src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().setup(|app| {#[cfg(mobile)]app.handle().plugin(tauri_plugin_biometric::Builder::new().build());Ok(())}).run(tauri::generate_context!()).expect("error while running tauri application");} -
使用你喜欢的 JavaScript 包管理器安装 JavaScript Guest 绑定:
npm install @tauri-apps/plugin-biometricyarn add @tauri-apps/plugin-biometricpnpm add @tauri-apps/plugin-biometricdeno add npm:@tauri-apps/plugin-biometricbun add @tauri-apps/plugin-biometric
在 iOS 系统上,生物识别插件需要 NSFaceIDUsageDescription 信息属性列表值,你应该在其中描述应用为何需要使用生物识别认证。
在 src-tauri/Info.ios.plist 文件中,添加以下代码片段:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"> <dict> <key>NSFaceIDUsageDescription</key> <string>Authenticate with biometric</string> </dict></plist>该插件使你能够验证设备上生物识别认证的可用性,向用户发起生物识别认证请求,并检查结果以确定认证是否成功。
你可以检查生物识别认证的状态,包括其可用性以及设备支持的生物识别认证方式类型。
import { checkStatus } from '@tauri-apps/plugin-biometric';
const status = await checkStatus();if (status.isAvailable) { console.log('Yes! Biometric Authentication is available');} else { console.log( 'No! Biometric Authentication is not available due to ' + status.error );}use tauri_plugin_biometric::BiometricExt;
fn check_biometric(app_handle: tauri::AppHandle) { let status = app_handle.biometric().status().unwrap(); if status.is_available { println!("Yes! Biometric Authentication is available"); } else { println!("No! Biometric Authentication is not available due to: {}", status.error.unwrap()); }}要提示用户进行生物识别认证,请使用 authenticate() 方法。
import { authenticate } from '@tauri-apps/plugin-biometric';
const options = { // 如果你希望用户能够使用手机密码进行身份验证,请设为 true allowDeviceCredential: false, cancelTitle: "Feature won't work if Canceled",
// 仅 iOS 平台的功能 fallbackTitle: 'Sorry, authentication failed',
// 仅 Android 平台的功能 title: 'Tauri feature', subtitle: 'Authenticate to access the locked Tauri function', confirmationRequired: true,};
try { await authenticate('This feature is locked', options); console.log( 'Hooray! Successfully Authenticated! We can now perform the locked Tauri function!' );} catch (err) { console.log('Oh no! Authentication failed because ' + err.message);}use tauri_plugin_biometric::{BiometricExt, AuthOptions};
fn bio_auth(app_handle: tauri::AppHandle) {
let options = AuthOptions { // 如果你希望用户能够使用手机密码进行身份验证,请设为 true allow_device_credential:false, cancel_title: Some("Feature won't work if Canceled".to_string()),
// 仅 iOS 平台的功能 fallback_title: Some("Sorry, authentication failed".to_string()),
// 仅 Android 平台的功能 title: Some("Tauri feature".to_string()), subtitle: Some("Authenticate to access the locked Tauri function".to_string()), confirmation_required: Some(true), };
// 如果认证成功,函数返回 Result::Ok() // 否则返回 Result::Error() match app_handle.biometric().authenticate("This feature is locked".to_string(), options) { Ok(_) => { println!("Hooray! Successfully Authenticated! We can now perform the locked Tauri function!"); } Err(e) => { println!("Oh no! Authentication failed because : {e}"); } }}默认情况下,所有潜在的危险插件命令和作用域均被阻止,无法访问。你必须修改 capabilities 配置中的权限才能启用这些功能。
有关更多信息,请参阅 功能概述, 关于如何使用插件权限,请参阅 分步指南。
{ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-capability", "description": "Capability for the main window", "windows": ["main"], "permissions": ["biometric:default"]}Default Permission
This permission set configures which biometric features are by default exposed.
Granted Permissions
It allows acccess to all biometric commands.
This default permission set includes the following:
allow-authenticateallow-status
Permission Table
| Identifier | Description |
|---|---|
|
|
Enables the authenticate command without any pre-configured scope. |
|
|
Denies the authenticate command without any pre-configured scope. |
|
|
Enables the status command without any pre-configured scope. |
|
|
Denies the status command without any pre-configured scope. |
© 2026 Tauri Contributors. CC-BY / MIT