4 Biometric

原文链接: https://tauri.app/plugin/biometric/

在 Android 和 iOS 上向用户发起生物识别认证。

支持的平台

平台支持程度说明
Windows不支持
Linux不支持
macOS不支持
Android完整支持
iOS完整支持

设置

自动

使用你的项目包管理器添加依赖:

1
npm run tauri add biometric
1
yarn run tauri add biometric
1
pnpm tauri add biometric
1
deno task tauri add biometric
1
bun tauri add biometric
1
cargo tauri add biometric

手动

  1. 在 src-tauri 文件夹中运行以下命令,把插件加入 Cargo.toml 里的项目依赖:

    1
    
    cargo add tauri-plugin-biometric --target 'cfg(any(target_os = "android", target_os = "ios"))'
    
  2. 修改 lib.rs 初始化插件:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    #[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");
    }
    
  3. 用你偏好的 JavaScript 包管理器安装 JavaScript 端绑定:

1
npm install @tauri-apps/plugin-biometric
1
yarn add @tauri-apps/plugin-biometric
1
pnpm add @tauri-apps/plugin-biometric
1
deno add npm:@tauri-apps/plugin-biometric
1
bun add @tauri-apps/plugin-biometric

配置

在 iOS 上,biometric 插件需要 NSFaceIDUsageDescription 信息属性列表值,它应当说明你的应用为什么需要使用生物识别认证。

在 src-tauri/Info.ios.plist 文件中加入以下片段:

1
2
3
4
5
6
7
8
<?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>

用法

该插件让你可以验证设备上生物识别认证是否可用、向用户发起生物识别认证,并检查结果以判断认证是否成功。

检查状态

你可以检查生物识别认证的状态,包括它是否可用以及支持哪些生物识别认证方式。

语言

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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
  );
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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() 方法。

语言

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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);
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use tauri_plugin_biometric::{BiometricExt, AuthOptions};

fn bio_auth(app_handle: tauri::AppHandle) {

    let options = AuthOptions {
        // 若希望用户可以使用手机密码认证,请设为 true
        allow_device_credential:false,
        cancel_title: Some("Feature won't work if Canceled".to_string()),

        // 仅 iOS 的特性
        fallback_title: Some("Sorry, authentication failed".to_string()),

        // 仅 Android 的特性
        title: Some("Tauri feature".to_string()),
        subtitle: Some("Authenticate to access the locked Tauri function".to_string()),
        confirmation_required: Some(true),
    };

    // 如果认证成功,函数返回 Result::Ok()
    // 否则返回 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 配置中修改权限才能启用它们。

更多信息请参阅能力概述,以及使用插件权限的分步指南。

1
2
3
4
5
6
7
{
  "$schema": "../gen/schemas/desktop-schema.json",
  "identifier": "main-capability",
  "description": "Capability for the main window",
  "windows": ["main"],
  "permissions": ["biometric:default"]
}
最后修改 September 26, 2026: 更新 (630b11f59)