跳转至正文
@capgo/capacitor-native-biometric 身份验证与安全 开源

原生生物识别 Capacitor 插件

使用 Face ID、Touch ID 和 Android 生物识别 API 实现安全身份验证

安装

bun add @capgo/capacitor-native-biometric bunx cap sync
npm、pnpm 或 yarn
  • npm install @capgo/capacitor-native-biometric
  • pnpm add @capgo/capacitor-native-biometric
  • yarn add @capgo/capacitor-native-biometric

指南

如何在 Capacitor 中使用 Native Biometric

在设备上测试

下载 Capgo App,然后扫描二维码 code。

Native Biometric 插件预览二维码 code

使用 @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 获取指定键受生物识别保护的值。该值必须是在 accessControl 设置为 BIOMETRY_CURRENT_SET 或 BIOMETRY_ANY 时存储的。
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()

删除指定服务器已存储的凭据。

import { NativeBiometric } from '@capgo/capacitor-native-biometric';

await NativeBiometric.deleteCredentials({ server: 'example.com' });

getSecureCredentials()

获取指定服务器已存储的凭据,此操作需要生物识别认证。凭据必须是在 accessControl 设置为 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-biometric 有关 @capgo/capacitor-native-biometric 的实现细节, 快速入门 有关“入门指南”中的实现细节, @capgo/capacitor-social-login 在 @capgo/capacitor-social-login 中查看实现细节, @capgo/capacitor-passkey 关于 @capgo/capacitor-passkey 的实现细节,以及 双因素认证 有关双重身份验证的实现细节。

常见问题

Native Biometric 插件常见问题

如何在 Capacitor 应用中安装 Native Biometric 插件?

运行 "bun add @capgo/capacitor-native-biometric"(或 "npm install @capgo/capacitor-native-biometric"),然后运行 "bunx cap sync",使 iOS 和 Android 项目同步原生 code。最后在你的应用 code 中从 "@capgo/capacitor-native-biometric" 导入即可。

@capgo/capacitor-native-biometric 是否支持 React、Vue 和 Angular?

支持。Native Biometric 是一个 Capacitor 包,因此它支持任何在 Capacitor 中运行的 Web 框架,包括 Ionic、React、Vue、Angular、Svelte 以及原生 JavaScript。

@capgo/capacitor-native-biometric 支持哪个版本的 Capacitor?

Capgo 插件遵循 Capacitor 的主版本号:请使用与 Capacitor 主版本号一致的插件主版本(例如插件 v8 对应 Capacitor 8)。GitHub README 中的兼容性表列出了仍在维护的版本。

@capgo/capacitor-native-biometric 是免费开源的吗?

是的。code 的源码已在 GitHub 公开,地址为 https://github.com/Cap-go/capacitor-native-biometric/,并且可以通过 npm 免费安装。欢迎提交 Bug 报告和 Pull Request。

我可以在不经过 App Store 审核的情况下,更新使用了 Native Biometric 的 code 吗?

安装或升级插件会改动原生 code,因此需要重新做一次商店构建。之后,调用该插件的 JavaScript、HTML 和 CSS 改动就能通过 Capgo live updates 立即发布。

无需等待应用审核,即可发布 Native Biometric 更新

只要插件已包含在你的商店版本中,Capgo live updates 就能在几分钟内把 JavaScript、HTML 和 CSS 的改动推送给用户。

开始使用 Capgo