跳转到内容
Tauri

生物识别

在 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 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>使用生物识别进行身份验证</string>
</dict>
</plist>

此插件允许你验证设备上生物识别验证的可用性,提示用户进行生物识别验证,并检查结果以确定验证是否成功。

你可以检查生物识别验证的状态,包括其可用性以及支持的生物识别验证方法的类型。

import { checkStatus } from '@tauri-apps/plugin-biometric';
const status = await checkStatus();
if (status.isAvailable) {
console.log('太棒了!生物识别验证可用');
} else {
console.log(
'哦不!由于以下原因,生物识别验证不可用:' + status.error
);
}

要提示用户进行生物识别验证,请使用 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);
}

默认情况下,所有潜在危险的插件命令和范围都被阻止,无法访问。你必须修改 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 acccess to all biometric commands.

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-status

Enables the status command without any pre-configured scope.

biometric:deny-status

Denies the status command without any pre-configured scope.


© 2025 Tauri Contributors. CC-BY / MIT