生物识别
提示用户在 Android 和 iOS 上进行生物特性认证。
¥Prompt the user for biometric authentication on Android and iOS.
支持的平台
¥Supported Platforms
This plugin requires a Rust version of at least 1.77.2
Platform | Level | Notes |
---|---|---|
windows | | |
linux | | |
macos | | |
android | ||
ios |
设置
¥Setup
安装生物识别插件即可开始使用。
¥Install the biometric plugin to get started.
使用项目的包管理器添加依赖:
¥Use your project’s package manager to add the dependency:
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
-
Run the following command in the
src-tauri
folder to add the plugin to the project’s dependencies inCargo.toml
:cargo add tauri-plugin-biometric --target 'cfg(any(target_os = "android", target_os = "ios"))' -
Modify
lib.rs
to initialize the plugin: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");} -
Install the JavaScript Guest bindings using your preferred JavaScript package manager:
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
配置
¥Configuration
在 iOS 上,生物识别插件需要 NSFaceIDUsageDescription
信息属性列表值,该值应描述你的应用为何需要使用生物识别身份验证。
¥On iOS the biometric plugin requires the NSFaceIDUsageDescription
information property list value, which should describe why your app needs to use biometric authentication.
在 src-tauri/Info.ios.plist
文件中,添加以下代码片段:
¥In the src-tauri/Info.ios.plist
file, add the following snippet:
<?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>
使用
¥Usage
此插件使你能够验证设备上的生物特性身份验证的可用性,提示用户进行生物特性身份验证,并检查结果以确定身份验证是否成功。
¥This plugin enables you to verify the availability of Biometric Authentication on a device, prompt the user for biometric authentication, and check the result to determine if the authentication was successful or not.
检查状态
¥Check Status
你可以检查生物特性身份验证的状态,包括其可用性和支持的生物特性身份验证方法类型。
¥You can check the status of Biometric Authentication, including its availability and the types of biometric authentication methods supported.
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
要提示用户进行生物特性身份验证,请使用 authenticate()
方法。
¥To prompt the user for Biometric Authentication, utilize the authenticate()
method.
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}"); } }}
权限
¥Permissions
默认情况下,所有潜在危险的插件命令和范围都会被阻止,无法访问。你必须修改 capabilities
配置中的权限才能启用这些权限。
¥By default all potentially dangerous plugin commands and scopes are blocked and cannot be accessed. You must modify the permissions in your capabilities
configuration to enable these.
有关更详细的说明,请参阅 功能概述。
¥See the Capabilities Overview for more information and the step by step guide to use plugin permissions.
{ "$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.
allow-authenticate
allow-status
Permission Table
Identifier | Description |
---|---|
|
Enables the authenticate command without any pre-configured scope. |
|
Denies the authenticate command without any pre-configured scope. |
|
Enables the status command without any pre-configured scope. |
|
Denies the status command without any pre-configured scope. |
Tauri 中文网 - 粤ICP备13048890号