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>
目录
相关文章
|
机器学习/深度学习 数据可视化 数据挖掘
|
新零售 监控
阿里云风险识别决策引擎发布
信息摘要: 风险识别推出风控一站式运营管理平台-决策引擎,助力于企业风控运营统筹管理与智能化升级。适用客户: 金融、电商零售、互动媒体、游戏娱乐等行业的企业用户版本/规格功能: 决策引擎提供个性化业务场景事件管理,可视化编排复杂决策,丰富的特征变量与场景识别服务、实时监控日志等功能,可满足多种业务场景下复杂策略的统筹运营与管理需求。
2531 0
excel判断单元格包含指定内容的函数用=IF(COUNTIF(A1,"*内容*"),"0","1")
  前面我们聊过怎样将Excel包含某字符的单元格填充颜色,这边我们用另外一种方法来实现:excel判断单元格包含指定内容的函数   选中需要显示结果的单元格,假设我们要判断第一行第一列的单元格A1是否含有“美女”一词,那我们在第一行第二列的单元格B1输入“=IF(COUNTIF(A1,"*美女*...
5194 0
|
负载均衡 算法 Java
Spring Cloud Alibaba - 12 使用Nacos的元数据实现金丝雀发布功能
Spring Cloud Alibaba - 12 使用Nacos的元数据实现金丝雀发布功能
456 0
|
机器学习/深度学习 算法 Python
Python实战|用决策树实现NBA获胜球队预测
Python实战|用决策树实现NBA获胜球队预测
|
存储 分布式计算 资源调度
|
9月前
|
前端开发 数据可视化 JavaScript
【五一创作】QML、Qt Quick /Qt中绘制圆形
【五一创作】QML、Qt Quick /Qt中绘制圆形
799 0
|
5月前
|
监控 关系型数据库 MySQL
zabbix agent集成percona监控MySQL的插件实战案例
这篇文章是关于如何使用Percona监控插件集成Zabbix agent来监控MySQL的实战案例。
132 2
zabbix agent集成percona监控MySQL的插件实战案例
|
数据安全/隐私保护
Qt之表单布局(QFormLayout)
简述 QFormLayout管理输入型控件和关联的标签组成的那些Form表单。 QFormLayout是一个方便的布局类,其中的控件以两列的形式被布局在表单中。左列包括标签,右列包含输入控件,例如:QLineEdit、QSpinBox等。 简述 使用 常用接口 总结 使用 我们可以通过addRow(const QString &amp;labelTe
2743 0