# AdaptationDsbridgeHarmonyOS **Repository Path**: jiaxingcode/adaptation-dsbridge-harmony-os ## Basic Information - **Project Name**: AdaptationDsbridgeHarmonyOS - **Description**: 适用于前端使用dsbridge框架和安卓,IOS进行通信。在前端不修改代码的情况下适配鸿蒙系统 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2024-04-02 - **Last Updated**: 2024-08-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 介绍 适用于前端使用dsbridge框架和安卓,IOS进行通信。在前端不修改代码的情况下适配鸿蒙系统 目前兼容Android、iOS、DSBridge库的核心功能 特性: - 支持h5调用native,回调传参数 - 支持native调用h5 源码: * [AdaptationDsbridgeHarmonyOS](https://gitee.com/jiaxingcode/adaptation-dsbridge-harmony-os.git) * [DSBridge-Android](https://github.com/wendux/DSBridge-Android) * [DSBridge-IOS](https://github.com/wendux/DSBridge-IOS) ## 安装 安装远程仓库: ```text ohpm install @jiaxing/ohos-adaptationdsbridge ``` ## 使用 创建自定义类JsBridgeMethod继承JsBridge ```typescript export class JsBridgeMethod extends JsBridge { private callbackId: number = 0 goNativeMethod(methodName: string, callBackMethodName: string, params: object) { //H5调用native if (methodName === 'getVsersion') { this.h5CallNative(methodName, callBackMethodName, params) } } h5CallNative(methodName: string, callBackMethodName: string, params: object) { //拼接返回给h5的数据格式 class GetVersion { data: object = new Object() status: number = 0 constructor(status: number, version: string) { this.status = status this.data["version"] = version } } //调用callBackToJs 将版本号传递给H5(第一个参数成功/失败状态,这里作为例子0为成功1为失败,第二个参数版本号) this.callBackToJs(callBackMethodName, new GetVersion(0, "111")) } /** * 调用H5端openDialog方法 * 传递参数hello * callbackId为自增的标识,不重复就好,没实际意义 */ nativeCallH5() { this.callbackId = this.callbackId++; let methodName: string = "openDialog" let params: string = "hello" this.controller.runJavaScript(`window._handleMessageFromNative({"method":"${methodName}","callbackId":${this.callbackId},"data":"${params}"})`) .then((result) => { //调用成功/失败的回调 }) } } ```