跳到主要內容
Tauri

生物辨識

提示使用者在 Android 和 iOS 上進行生物辨識驗證。

支援平台

此外掛程式需要 Rust 版本至少 1.77.2

平台 等級 備註
windows
linux
macos
android
ios

設定

安裝生物辨識外掛程式以開始使用。

使用您專案的套件管理器新增依賴項

npm run tauri add biometric

組態

在 iOS 上,生物辨識外掛程式需要 NSFaceIDUsageDescription 資訊屬性列表值,該值應描述您的應用程式為何需要使用生物辨識驗證。

src-tauri/Info.ios.plist 檔案中,新增以下程式碼片段

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
);
}

驗證身分

若要提示使用者進行生物辨識驗證,請使用 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);
}

權限

預設情況下,所有潛在危險的外掛程式命令和範圍都會被封鎖且無法存取。您必須修改 capabilities 組態中的權限才能啟用這些。

請參閱 功能總覽 以取得更多資訊,以及 逐步指南 以使用外掛程式權限。

src-tauri/capabilities/default.json
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "main-capability",
"description": "Capability for the main window",
"windows": ["main"],
"permissions": ["biometric:default"]
}

預設權限

此權限設定預設公開哪些生物辨識功能。

已授與的權限

它允許存取所有生物辨識命令。

  • allow-authenticate
  • allow-status

權限表

識別碼 描述

biometric:allow-authenticate

啟用 authenticate 命令,無需任何預先設定的範圍。

biometric:deny-authenticate

拒絕 authenticate 命令,無需任何預先設定的範圍。

biometric:allow-status

啟用 status 命令,無需任何預先設定的範圍。

biometric:deny-status

拒絕 status 命令,無需任何預先設定的範圍。


© 2025 Tauri 貢獻者。CC-BY / MIT