WebViewJavascriptBridge

简介:

前段时间做h5交互使用了WebViewJavascriptBridge,今天做下整理。



首先确保一份已经配好功能的html文件。(html还在学习阶段,暂时就不卖弄了。。。)

 1.初始化一个webview(viewdidload)

<span class="constant">UIWebView</span>* webView = [[<span class="constant">UIWebView</span> alloc] <span class="symbol" style="color: rgb(153, 0, 115);">initWithFrame:</span><span class="keyword" style="font-weight: bold;">self</span>.view.bounds];
    [<span class="keyword" style="font-weight: bold;">self</span>.view <span class="symbol" style="color: rgb(153, 0, 115);">addSubview:</span>webView];

 2.将此webview与WebViewJavascriptBridge关联(viewdidload)

<span class="keyword" style="font-weight: bold;">if</span> (_bridge) { <span class="keyword" style="font-weight: bold;">return</span>; }

<span class="indent">  </span>[WebViewJavascriptBridge enableLogging];
<span class="indent">  </span>
<span class="indent">  </span>_bridge = [WebViewJavascriptBridge bridgeForWebView:webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
<span class="indent">  </span><span class="indent">  </span>NSLog(<span class="variable" style="color: rgb(0, 128, 128);">@"</span>ObjC received message from JS: <span class="variable" style="color: rgb(0, 128, 128);">%@</span><span class="string" style="color: rgb(221, 17, 68);">", data);
<span class="indent">  </span><span class="indent">  </span>
<span class="indent">  </span><span class="indent">  </span>responseCallback(<span class="variable" style="color: rgb(0, 128, 128);">@"</span>Response for message from ObjC"</span>);
<span class="indent">  </span>}];

 ps:此时你的webview就与js搭上桥了。下面就是方法的互调和参数的互传。

 (1) js调oc方法(可以通过data给oc方法传值,使用responseCallback将值再返回给js)

[_bridge registerHandler:<span class="variable" style="color: rgb(0, 128, 128);">@"</span>testObjcCallback<span class="string" style="color: rgb(221, 17, 68);">" handler:^(id data, WVJBResponseCallback responseCallback) {
        </span><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;">
</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(112, 61, 170);">NSData</span><span style="color: rgb(0, 0, 0);"> *jsonData = [data </span>dataUsingEncoding<span style="color: rgb(0, 0, 0);">:</span>NSUTF8StringEncoding<span style="color: rgb(0, 0, 0);">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span style="color: rgb(0, 0, 0);">        </span>NSDictionary<span style="color: rgb(0, 0, 0);"> *dic = [</span>NSJSONSerialization<span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(61, 29, 129);">JSONObjectWithData</span><span style="color: rgb(0, 0, 0);">:jsonData</span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: rgb(0, 0, 0);">                            </span>options<span style="color: rgb(0, 0, 0);">:</span>NSJSONReadingMutableContainers<span style="color: rgb(0, 0, 0);"> </span>error<span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(187, 44, 162);">nil</span><span style="color: rgb(0, 0, 0);">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;">        </p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">        [<span style="color: rgb(187, 44, 162);">self</span> <span style="color: rgb(49, 89, 93);">reloadNavgation</span>:dic];     <span style="color: rgb(0, 132, 0);">//</span><span style="line-height: normal; font-family: 'Heiti SC Light'; color: rgb(0, 132, 0);">跳转</span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);"><span style="color: rgb(0, 0, 0);">        responseCallback(</span>@"Response from iHfPayCalledByJs"<span style="color: rgb(0, 0, 0);">);</span></p><span class="string" style="color: rgb(221, 17, 68);">
    }];</span>

  这里注意testObjcCallback这个方法的标示。html那边的命名要跟ios这边相同,才能调到这个方法。当然这个名字可以两边商量着自定义。简单明确即可。

  (2)oc调js方法(通过data可以传值,通过  response可以接受js那边的返回值 )

id data = @{ <span class="variable" style="color: rgb(0, 128, 128);">@"</span>greetingFromObjC<span class="string" style="color: rgb(221, 17, 68);">": <span class="variable" style="color: rgb(0, 128, 128);">@"</span>Hi there, JS!"</span> };
    [_bridge callHandler:<span class="variable" style="color: rgb(0, 128, 128);">@"</span>testJavascriptHandler<span class="string" style="color: rgb(221, 17, 68);">" data:data responseCallback:^(id response) {
        NSLog(<span class="variable" style="color: rgb(0, 128, 128);">@"</span>testJavascriptHandler responded: <span class="variable" style="color: rgb(0, 128, 128);">%@</span>"</span>, response);
    }];

 注意这里的  testJavascriptHandler也是个方法标示。

 (3)oc给js传值(通过  response接受返回值 )

[_bridge <span class="keyword" style="font-weight: bold;">send</span>:<span class="variable" style="color: rgb(0, 128, 128);">@"</span>A string sent from ObjC to JS<span class="string" style="color: rgb(221, 17, 68);">" responseCallback:^(id response) {
        NSLog(<span class="variable" style="color: rgb(0, 128, 128);">@"</span>sendMessage got response: <span class="variable" style="color: rgb(0, 128, 128);">%@</span>"</span>, response);
    }];

  (4)oc给js传值(无返回值)

<span class="title" style="color: rgb(153, 0, 0); font-weight: bold;">[_bridge send:@"A string sent from ObjC after Webview has loaded."]</span><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">;</span>
目录
相关文章
|
4月前
|
人工智能 自然语言处理 安全
阿里云Qoder CN能做什么?只是代码补全还是能做更多?
阿里云Qoder CN(原灵码)是覆盖开发与办公全场景的AI智能体系列,含IDE插件、桌面助手、CLI终端、数字员工及云端Agent等形态,支持自主编程、专家协作、语音交互、安全扫描及跨端任务管理,助力高效智能工作。阿里云Qoder CN官网:https://t.aliyun.com/U/hGZKNX
|
9月前
|
传感器 数据采集 数据可视化
基于STM32的智能家居控制系统设计
基于STM32的智能家居控制系统设计
992 10
|
6月前
|
安全 应用服务中间件 网络安全
阿里云SSL证书全流程指南:从申请到Nginx/Tomcat/IIS生产级部署
本文系统讲解阿里云SSL证书从申请到部署的完整技术流程。首先阐明HTTPS加密的必要性与SSL/TLS协议原理,深入对比个人测试证书(免费DV)、付费OV/EV证书的差异与选型策略。随后详解证书申请全流程,涵盖自动DNS验证与手动DNS验证两种方式。核心部分分别针对Nginx、Tomcat、IIS三大主流Web服务器,给出证书下载、格式转换、配置文件修改、服务重启到验证测试的完整操作步骤与代码示例。最后总结443端口开放、证书链完整性、私钥权限、安全组配置等常见部署问题及解决方案,帮助读者一步到位完成HTTPS加密配置。
|
6月前
|
安全 Java C++
Java 集合框架源码核心解析
Java集合框架源码核心解析,聚焦面试与实战高频点:ArrayList(动态数组、1.5倍扩容)、LinkedList(双向链表)、HashMap(数组+链表+红黑树、CAS不安全)、ConcurrentHashMap(CAS+synchronized线程安全),直击底层原理与性能本质。(239字)
393 0
|
人工智能 自然语言处理 安全
零成本打造智能服务端:MCP采样的降本增效实践
本文介绍MCP采样机制,突破传统单向调用模式,实现服务器与客户端LLM的双向协作,提升扩展性、降低成本,支持灵活模型选择。通过FastMCP框架,打造高效分布式AI计算架构。
918 8
|
人工智能 Android开发 iOS开发
安卓版快捷指令,加了AI语音可以一句话操作v0.2.7
Shortcuts for Android(SFA)是一款安卓自动化工具,支持语音创建快捷指令,实现听歌、导航、发消息等操作。操作简单,提升效率,快来体验语音控制的便捷!
1738 0
安卓版快捷指令,加了AI语音可以一句话操作v0.2.7
|
人工智能 自然语言处理 安全
中文竞技场-中文大模型比比看
今天,大语言模型正在各个应用领域引起巨大的变革,并已经在搜索、金融、办公、安全、教育、游戏、电商、社交媒体等领域迅速普及和应用。例如微软将 GPT4应用于必应搜索引擎和 Office 办公软件。几乎每个企业都试图探索如何将AI融入业务和技术中去。但以中文为主的语言大模型却缺少应有的关注,今天让我们聚焦中文竞技场,看看各种中文大语言模型的表现吧~
64704 1
中文竞技场-中文大模型比比看
|
Python
pandas 生成 Excel 时的 sheet 问题
pandas 生成 Excel 时的 sheet 问题
857 1
|
Linux
配置时间同步服务
在Redhat 9.2上,Chrony被用来同步系统时间与NTP服务器。默认情况下,它已在RHEL7/CentOS7中预装。要安装,首先通过`yum -y remove chrony`卸载,然后用`yum -y install chrony`进行安装。安装后,使用`systemctl restart chronyd`重启服务,并用`systemctl enable chronyd`设置开机启动。编辑`/etc/chrony.conf`添加阿里云NTP服务器如`ntp1.aliyun.com`和`ntp2.aliyun.com`以同步时间。使用`chronyc sources -n`确认时间源。
2027 4

热门文章

最新文章