生物识别
在 Android 和 iOS 上提示用户进行生物识别认证。
支持的平台
此插件需要至少 1.77.2 版本的 Rust
平台 | 等级 | 注释 |
---|---|---|
Windows | | |
Linux | | |
macOS | | |
Android | ||
iOS |
设置
安装生物识别插件开始使用。
使用您项目的包管理器添加依赖项
npm run tauri add biometric
yarn run tauri add biometric
pnpm tauri add biometric
deno task tauri add biometric
bun tauri add biometric
cargo 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 客户端绑定
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 = { // Set true if you want the user to be able to authenticate using phone password allowDeviceCredential: false, cancelTitle: "Feature won't work if Canceled",
// iOS only feature fallbackTitle: 'Sorry, authentication failed',
// Android only features 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 { // 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"]}
默认权限
此权限集配置了默认暴露哪些生物识别功能。
授予的权限
允许访问所有生物识别命令。
allow-authenticate
allow-status
权限表
标识符 | 描述 |
---|---|
|
启用authenticate命令而不需要预先配置的作用域。 |
|
拒绝authenticate命令而不需要预先配置的作用域。 |
|
启用status命令而不需要预先配置的作用域。 |
|
拒绝status命令而不需要预先配置的作用域。 |
© 2025 Tauri 投资者。CC-BY / MIT