生物识别
在 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>使用生物识别进行身份验证</string> </dict></plist>此插件允许你验证设备上生物识别验证的可用性,提示用户进行生物识别验证,并检查结果以确定验证是否成功。
你可以检查生物识别验证的状态,包括其可用性以及支持的生物识别验证方法的类型。
import { checkStatus } from '@tauri-apps/plugin-biometric';
const status = await checkStatus();if (status.isAvailable) { console.log('太棒了!生物识别验证可用');} else { console.log( '哦不!由于以下原因,生物识别验证不可用:' + 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!("太棒了!生物识别验证可用"); } else { println!("哦不!由于以下原因,生物识别验证不可用:{}", status.error.unwrap()); }}要提示用户进行生物识别验证,请使用 authenticate() 方法。
import { authenticate } from '@tauri-apps/plugin-biometric';
const options = { // 如果你想让用户能够使用手机密码进行身份验证,请设置为 true allowDeviceCredential: false, cancelTitle: "如果取消,功能将无法使用",
// 仅限 iOS 功能 fallbackTitle: '抱歉,身份验证失败',
// 仅限 Android 功能 title: 'Tauri 功能', subtitle: '进行身份验证以访问锁定的 Tauri 功能', confirmationRequired: true,};
try { await authenticate('此功能已锁定', options); console.log( '万岁!验证成功!我们现在可以执行锁定的 Tauri 功能了!' );} catch (err) { console.log('哦不!验证失败,因为 ' + err.message);}use tauri_plugin_biometric::{BiometricExt, AuthOptions};
fn bio_auth(app_handle: tauri::AppHandle) {
let options = AuthOptions { // Set True if you want the user to be able to authenticate using phone password allow_device_credential:false, cancel_title: Some("Feature won't work if Canceled".to_string()),
// iOS only feature fallback_title: Some("Sorry, authentication failed".to_string()),
// Android only features title: Some("Tauri feature".to_string()), subtitle: Some("Authenticate to access the locked Tauri function".to_string()), confirmation_required: Some(true), };
// if the authentication was successful, the function returns Result::Ok() // otherwise returns 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. |
© 2025 Tauri Contributors. CC-BY / MIT