生物辨識
提示使用者在 Android 和 iOS 上進行生物辨識驗證。
支援平台
此外掛程式需要 Rust 版本至少 1.77.2
平台 | 等級 | 備註 |
---|---|---|
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 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 = { // 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