使用@capgo/capacitor-native-biometric
该插件为Android和iOS提供原生生物识别API
安装
bun add @capgo/capacitor-native-biometric
bunx cap sync
bunx cap sync 将插件的本机code复制到您的本机项目中。每次升级插件后,请再次运行它。
导入
import { NativeBiometric } from '@capgo/capacitor-native-biometric';
API概览
| 方法 | 描述 |
|---|---|
isAvailable |
检查生物识别认证硬件是否可用。 |
verifyIdentity |
提示用户使用生物识别进行认证。 |
getCredentials |
获取给定服务器的存储凭据。 |
setCredentials |
为给定服务器存储指定的凭据。 |
deleteCredentials |
删除给定服务器的存储凭据。 |
getSecureCredentials |
获取给定服务器的存储凭据,需要生物识别认证。凭据必须已存储并且accessControl设置为BIOMETRY_CURRENT_SET或BIOMETRY_ANY。 |
isCredentialsSaved |
检查是否已为给定服务器保存凭据。 |
setData |
以给定的键存储一个任意的字符串值。值在使用平台的安全存储后端(Android Keystore + SharedPreferences,iOS Keychain)进行加密。 |
getData |
获取给定键的之前存储的值。只返回未使用生物识别的值。 accessControl. |
getSecureData |
获取给定键的生物识别保护的值。该值必须使用BIOMETRY_CURRENT_SET或BIOMETRY_ANY设置。 accessControl 删除给定键的存储值(保护和不保护)。 |
deleteData |
检查是否已经为给定键保存了值。 |
isDataSaved |
示例 |
检查生物识别硬件是否可用。
isAvailable()
提示用户进行生物识别认证。
import { NativeBiometric } from '@capgo/capacitor-native-biometric';
const result = await NativeBiometric.isAvailable();
console.log(result);
verifyIdentity()
获取给定服务器的存储凭据。
import { NativeBiometric } from '@capgo/capacitor-native-biometric';
await NativeBiometric.verifyIdentity();
getCredentials()
存储给定服务器的凭据。
import { NativeBiometric } from '@capgo/capacitor-native-biometric';
const result = await NativeBiometric.getCredentials({ server: 'example.com' });
// The result holds sensitive values: use it without logging it.
setCredentials()
删除给定服务器的存储凭据。
import { NativeBiometric } from '@capgo/capacitor-native-biometric';
await NativeBiometric.setCredentials({
username: 'username',
password: 'password',
server: 'example.com',
});
deleteCredentials()
Examples
import { NativeBiometric } from '@capgo/capacitor-native-biometric';
await NativeBiometric.deleteCredentials({ server: 'example.com' });
getSecureCredentials()
获取指定服务器的存储凭据,需要生物识别验证。凭据必须已设置访问控制为BIOMETRY_CURRENT_SET或BIOMETRY_ANY。
import { NativeBiometric } from '@capgo/capacitor-native-biometric';
const result = await NativeBiometric.getSecureCredentials({ server: 'example.com' });
// The result holds sensitive values: use it without logging it.
上表列出了12个核心方法。监听器和版本辅助函数,以及每个方法的完整契约,均在 GitHub仓库.
监听事件
addListener 返回句柄。调用 handle.remove() 当屏幕卸载时。
完整参考
继续使用@capgo/capacitor-native-biometric
如果您正在使用 使用@capgo/capacitor-native-biometric 规划身份验证和帐户流程,连接它与 @capgo/capacitor-native-生物识别 查看@capgo/capacitor-native-biometric的实现细节 开始使用 查看开始使用的实现细节 @capgo/capacitor-社会登录 查看@capgo/capacitor-social-login的实现细节 @capgo/capacitor-指纹识别 查看@capgo/capacitor-passkey的实现细节,并 双因素身份验证 查看双因素身份验证的实现细节