@bingtang-rn/react-native-simple-biometrics
v3.0.2
Published
适配鸿蒙版本,提供react_native_simple_biometrics组件。
Downloads
102
Maintainers
Readme
@bingtang-rn/react-native-simple-biometrics for HarmonyOS
本项目基于 react-native-simple-biometrics 开发,为 React Native 鸿蒙(OpenHarmony)适配版本。如果在使用过程中有任何问题,可以在GitCode提Issue,会及时跟进。issue地址:issues。
版本对应关系
| 鸿蒙适配包版本 | 原始库版本 | 支持 RN 版本 | Autolink | 编译 API 版本 | | ------------ | ---------- | ------------ | -------- | ------------- | | 见发布记录 | 3.0.2 | 0.72+ | 否 | API17+ |
安装
npm install @bingtang-rn/react-native-simple-biometrics使用
import RNBiometrics from 'react-native-simple-biometrics';
// 检查设备是否支持生物识别
const canAuth = await RNBiometrics.canAuthenticate();
// 发起生物识别认证
try {
const success = await RNBiometrics.requestBioAuth('Security', 'Authenticate to View');
// success === true 认证成功
} catch (err) {
// 认证失败或取消
}import 时使用原库名
'react-native-simple-biometrics',而非鸿蒙包名。
平台差异:
promptMessage参数在 HarmonyOS 认证界面中作为导航按钮文本(navigationButtonText)展示,而非副标题。这是因为鸿蒙WidgetParam只有title和navigationButtonText两个字段,无subtitle/message字段。
权限要求:
- 需在
module.json5声明ohos.permission.ACCESS_BIOMETRIC(system_grant 级别,安装时自动授权)
Link
| 版本 | 是否支持 Autolink | |------|------------------| | 当前版本 | 否(需手动注册) |
如使用版本支持 Autolink 且工程已接入,可跳过手动配置。
说明:本模块需要同时在 C++ 侧和 ETS 侧注册 Package。
1. Overrides RN SDK
在工程根目录 oh-package.json5 添加:
{
"overrides": {
"@rnoh/react-native-openharmony": "./react_native_openharmony"
}
}2. 引入原生端依赖
打开 entry/oh-package.json5,添加:
"dependencies": {
"@bingtang-rn/react-native-simple-biometrics": "file:../../node_modules/@bingtang-rn/react-native-simple-biometrics/harmony/simple_biometrics.har"
}执行 ohpm install。
3. 配置 CMakeLists
打开 entry/src/main/cpp/CMakeLists.txt,添加:
set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
add_subdirectory("${OH_MODULES}/@bingtang-rn/react-native-simple-biometrics/src/main/cpp" ./simple_biometrics)
target_link_libraries(rnoh_app PUBLIC simple_biometrics)4. 注册 Package(C++ 侧)
打开 entry/src/main/cpp/PackageProvider.cpp,添加:
#include "SimpleBiometricsPackage.h"
std::vector<std::shared_ptr<Package>> PackageProvider::getPackages(Package::Context ctx) {
return {
std::make_shared<SimpleBiometricsPackage>(ctx),
};
}5. 注册 Package(ETS 侧)
打开 entry/src/main/ets/RNPackagesFactory.ets,添加:
import { SimpleBiometricsPackage } from '@bingtang-rn/react-native-simple-biometrics/ts';
export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
return [
new SimpleBiometricsPackage(ctx),
];
}属性 / API
| API | 描述 | 参数 | 返回值 | HarmonyOS 支持 | |-----|------|------|--------|----------------| | canAuthenticate | 检查设备是否支持生物识别认证 | allowDeviceCredentials?: boolean(默认 true) | Promise | ✅ 完全支持 | | requestBioAuth | 发起生物识别认证弹窗 | promptTitle: string, promptMessage: string, allowDeviceCredentials?: boolean(默认 true) | Promise | ⚠️ 部分支持 |
平台差异
promptMessage参数:Android/iOS 作为认证弹窗副标题展示;HarmonyOS 上作为导航按钮文本(navigationButtonText)展示,因为鸿蒙WidgetParam只有title和navigationButtonText两个字段,无subtitle/message字段
使用限制
- 需在
module.json5声明ohos.permission.ACCESS_BIOMETRIC(system_grant 级别,安装时自动授权) canAuthenticate实现为逐一尝试getAvailableStatus(authType, ATL2),任一可用即返回 true;鸿蒙 API 失败时抛异常而非返回布尔值,已用 try-catch 包装
快速验证(运行 Example)
前置条件
| 依赖 | 版本要求 | |------|----------| | Node.js | >= 18 | | DevEco Studio | 5.0+ / 6.0+ | | HarmonyOS SDK | API 17+ |
运行步骤
1. 克隆仓库
git clone <仓库地址>
cd <仓库目录>2. 安装依赖并构建
npm install --legacy-peer-deps
npm pack # 生成 tgz 包(会自动触发 prepare 构建 JS 产物)3. 进入 example 目录,安装依赖
cd example
npm install --legacy-peer-deps4. 生成 JS Bundle
npm run dev产物:harmony/entry/src/main/resources/rawfile/bundle.harmony.js
5. 用 DevEco Studio 打开鸿蒙工程
- 打开 DevEco Studio
- 选择
example/harmony目录 - 等待 Sync 完成
6. 编译并运行 HAP
在 DevEco Studio 中点击运行按钮,将 HAP 安装到设备/模拟器。
注意:Example 中已预置插件依赖和 Package 注册,无需手动配置 Link。
约束与限制
兼容性
- RNOH: 0.72+
- HarmonyOS SDK: API 17+
- DevEco Studio: 5.0+
遗留问题
无
开源协议
本项目基于 MIT License,详见 LICENSE 文件。
