【HarmonyOS 5】鸿蒙Web组件和内嵌网页双向通信DEMO示例

GeorgeGcs
• 阅读 3

##鸿蒙开发能力 ##HarmonyOS SDK应用服务##鸿蒙金融类应用 (金融理财# 一、前言 在 ArkUI 开发中,Web 组件(Web)允许开发者在应用内嵌入网页,实现混合开发场景。 本文将通过完整 DEMO,详解如何通过WebviewController实现 ArkUI 与内嵌网页的双向通信,涵盖 ArkUI 调用网页 JS、网页调用 ArkUI 对象的核心技术点。 二、双向通信实现原理 1、双向通信概念 Web 到 ArkUI(反向通信)通过registerJavaScriptProxy将 ArkUI 对象注册到网页的window对象,允许网页通过window.xxx调用 ArkUI 暴露的方法。 ArkUI 到 Web(正向通信)通过runJavaScript执行网页 JS 代码,支持回调获取返回值,实现原生代码调用网页函数。 2、双向通信流程图 ArkUI Web ┌──────────────┐ ┌──────────────┐ │ registerJS ├─────────────▶ window.objName │ │ (反向注册) │ ├──────────────┤ ├──────────────┤ │ call test() │ │ runJavaScript├─────────────▶ execute JS code │ │ (正向调用) │ ├──────────────┤ └──────────────┘ └──────────────┘

三、双向通信实现步骤 1、ArkUI 定义可被网页调用的对象 创建一个TestObj类,声明允许网页调用的方法(白名单机制): class TestObj { // 网页可调用的方法1:返回字符串 test(): string { return "ArkUI Web Component"; }

// 网页可调用的方法2:打印日志 toString(): void { console.log('Web Component toString'); }

// 网页可调用的方法3:接收网页消息 receiveMessageFromWeb(message: string): void { console.log(Received from web: ${message}); } } 2、ArkUI 组件核心代码 初始化控制器与状态 @Entry @Component struct WebComponent { // Webview控制器 controller: webview.WebviewController = new webview.WebviewController(); // 注册到网页的ArkUI对象 @State testObj: TestObj = new TestObj(); // 注册名称(网页通过window.[name]访问) @State regName: string = 'objName'; // 接收网页返回数据 @State webResult: string = '';

build() { /* 组件布局与交互逻辑 */ } } 布局与交互按钮,添加三个核心功能按钮: Column() { // 显示网页返回数据 Text(Web返回数据:${this.webResult}).fontSize(16).margin(10);

// 1. 注册ArkUI对象到网页 Button('注册到Window') .onClick(() => { this.controller.registerJavaScriptProxy( this.testObj, // ArkUI对象 this.regName, // 网页访问名称 ["test", "toString", "receiveMessageFromWeb"] // 允许调用的方法白名单 ); })

// 2. ArkUI调用网页JS Button('调用网页函数') .onClick(() => { this.controller.runJavaScript( 'webFunction("Hello from ArkUI!")', // 执行网页JS代码 (error, result) => { // 回调处理返回值 if (!error) this.webResult = result || '无返回值'; } ); })

// 3. Web组件加载 Web({ src: $rawfile('index.html'), controller: this.controller }) .javaScriptAccess(true) // 开启JS交互权限 .onPageEnd(() => { // 页面加载完成时触发 // 页面加载后自动调用网页测试函数 this.controller.runJavaScript('initWebData()'); }) } 3. registerJavaScriptProxy的实际作用是,将 ArkUI 对象绑定到网页window,实现反向通信。 registerJavaScriptProxy( obj: Object, // ArkUI中定义的对象 name: string, // 网页访问的名称(如window.name) methods: string[] // 允许调用的方法白名单(严格匹配方法名) ); 源码示例 完整代码已上传至Gitee 仓库,欢迎下载调试!如果有任何问题,欢迎在评论区留言交流~ 项目结构 ├── xxx.ets # ArkUI组件代码 └── index.html # 内嵌网页文件

WebViewPage.ets import { webview } from '@kit.ArkWeb'; import { BusinessError } from '@kit.BasicServicesKit';

class TestObj { constructor() { }

test(): string { return "ArkUI Web Component"; }

toString(): void { console.log('Web Component toString'); }

receiveMessageFromWeb(message: string): void { console.log(Received message from web: ${message}); } }

@Entry @Component struct WebViewPage { controller: webview.WebviewController = new webview.WebviewController(); @State testObjtest: TestObj = new TestObj(); @State name: string = 'objName'; @State webResult: string = '';

build() { Column() { Text(this.webResult).fontSize(20) Button('refresh') .onClick(() => { try { this.controller.refresh(); } catch (error) { console.error(ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}); } }) Button('Register JavaScript To Window') .onClick(() => { try { this.controller.registerJavaScriptProxy(this.testObjtest, this.name, ["test", "toString", "receiveMessageFromWeb"]); } catch (error) { console.error(ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}); } }) Button('deleteJavaScriptRegister') .onClick(() => { try { this.controller.deleteJavaScriptRegister(this.name); } catch (error) { console.error(ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}); } }) Button('Send message to web') .onClick(() => { try { this.controller.runJavaScript( 'receiveMessageFromArkUI("Hello from ArkUI!")', (error, result) => { if (error) { console.error(run JavaScript error, ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}); return; } console.info(Message sent to web result: ${result}); } ); } catch (error) { console.error(ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}); } }) Button('Get data from web') .onClick(() => { try { this.controller.runJavaScript( 'getWebPageData()', (error, result) => { if (error) { console.error(run JavaScript error, ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}); return; } if (result) { this.webResult = result; console.info(Data from web: ${result}); } } ); } catch (error) { console.error(ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}); } }) Web({ src: $rawfile('index.html'), controller: this.controller }) .javaScriptAccess(true) .onPageEnd(e => { try { this.controller.runJavaScript( 'test()', (error, result) => { if (error) { console.error(run JavaScript error, ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}); return; } if (result) { this.webResult = result; console.info(The test() return value is: ${result}); } } ); if (e) { console.info('url: ', e.url); } } catch (error) { console.error(ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}); } }) .width("100%") .height("50%") } .width("100%") .height("100%") .backgroundColor(Color.Black) } }

index.html

Web Communication

注意 组件销毁时调用deleteJavaScriptRegister(name)取消注册,避免内存泄漏: onDestroy() { this.controller.deleteJavaScriptRegister(this.regName); }

点赞
收藏
评论区
推荐文章
GeorgeGcs GeorgeGcs
16小时前
【 HarmonyOS 5 入门系列 】鸿蒙HarmonyOS示例项目讲解
【HarmonyOS5入门系列】鸿蒙HarmonyOS示例项目讲解\鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、前言:移动开发声明式UI框架的技术变革在移动操作系统的发展历程中,UI开发模式经历了从命令式到声明式的重大变革。根据
GeorgeGcs GeorgeGcs
16小时前
【HarmonyOS 5】AttributeModifier和AttributeUpdater区别详解
【HarmonyOS5】AttributeModifier和AttributeUpdater区别详解\鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、AttributeModifier和AttributeUpdater的定义和作用1
GeorgeGcs GeorgeGcs
8小时前
【HarmonyOS 5】鸿蒙中Stage模型与FA模型详解
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、前言在HarmonyOS5的应用开发模型中,featureAbility是旧版FA模型(FeatureAbility)的用法,Stage模型已采用全新的应用架构,推荐使用组件化的上下文
GeorgeGcs GeorgeGcs
8小时前
【HarmonyOS 5】桌面快捷方式功能实现详解
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、前言在移动应用开发中,如何让用户快速触达核心功能,是目前很常见的功能之一。鸿蒙系统提供的桌面快捷方式(Shortcuts)功能,允许开发者为应用内常用功能创建直达入口,用户通过长按应用
GeorgeGcs GeorgeGcs
8小时前
【HarmonyOS 5】鸿蒙中Stage模型与FA模型详解
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、前言在HarmonyOS5的应用开发模型中,featureAbility是旧版FA模型(FeatureAbility)的用法,Stage模型已采用全新的应用架构,推荐使用组件化的上下文
GeorgeGcs GeorgeGcs
8小时前
【HarmonyOS 5】鸿蒙组件&模板服务详解 - 助力高效开发的利器
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、前言在移动应用开发领域,效率与质量始终是开发者追求的核心目标。鸿蒙系统作为新兴的操作系统,为开发者提供了丰富且强大的开发资源,其中鸿蒙组件&模板服务更是成为开发者快速构建高质量应用的得
GeorgeGcs GeorgeGcs
8小时前
【HarmonyOS 5】使用openCustomDialog如何禁止手势关闭的方案
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、前言在HarmonyOS中使用openCustomDialog自定义弹框时,我们会遇到实现禁止手势关闭弹框的业务场景。虽然在HarmonyOSNext中,自定义Dialog默认可能继承
GeorgeGcs GeorgeGcs
8小时前
【HarmonyOS 5】鸿蒙中@State的原理详解
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、@State在鸿蒙中是做什么的?@State是HarmonyOSArkTS框架中用于管理组件状态的核心装饰器,其核心作用是实现数据驱动UI的响应式编程模式。通过将变量标记为@State
GeorgeGcs GeorgeGcs
6小时前
【HarmonyOS 5】如何开启DevEco Studio热更新调试应用模式
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、AttributeModifier和AttributeUpdater的定义和作用1.AttributeModifier是ArkUI组件的动态属性,提供属性设置功能。开发者可使用attr
GeorgeGcs GeorgeGcs
6小时前
【HarmonyOS 5】鸿蒙中进度条的使用详解
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、HarmonyOS中Progress进度条的类型HarmonyOS的ArkUI框架为开发者提供了多种类型的进度条,每种类型都有其独特的样式,以满足不同的设计需求。以下是几种常见的进度条
GeorgeGcs
GeorgeGcs
Lv1
男 · 金融头部企业 · 鸿蒙应用架构师
HarmonyOS认证创作先锋,华为HDE专家,鸿蒙讲师,作者。目前任职鸿蒙应用架构师。 历经腾讯,宝马,研究所,金融。 待过私企,外企,央企。 深耕大应用开发领域十年。 AAE,Harmony(OpenHarmony\HarmonyOS),MAE(Android\IOS),FE(H5\Vue\RN)。
文章
56
粉丝
1
获赞
2