跳转到内容

生物识别

GitHubnpmcrates.io
API Reference:

在 Android 和 iOS 设备上向用户弹出生物识别认证。

This plugin requires a Rust version of at least 1.90

PlatformLevelNotes
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 = {
// 如果你希望用户能够使用手机密码进行身份验证,请设为 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);
}

默认情况下,所有潜在的危险插件命令和作用域均被阻止,无法访问。你必须修改 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"]
}

Default Permission

This permission set configures which biometric features are by default exposed.

Granted Permissions

It allows checking the biometric status and prompting for authentication. Encrypting and decrypting data with biometric_cipher is not included and must be enabled explicitly with biometric:allow-biometric-cipher.

This default permission set includes the following:

  • allow-authenticate
  • allow-status

Permission Table

Identifier Description

biometric:allow-authenticate

Enables the authenticate command without any pre-configured scope.

biometric:deny-authenticate

Denies the authenticate command without any pre-configured scope.

biometric:allow-biometric-cipher

Enables the biometric_cipher command without any pre-configured scope.

biometric:deny-biometric-cipher

Denies the biometric_cipher command without any pre-configured scope.

biometric:allow-status

Enables the status command without any pre-configured scope.

biometric:deny-status

Denies the status command without any pre-configured scope.


© 2026 Tauri Contributors. CC-BY / MIT