前段时间做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>