由于开发需要OC或Java与JS间的交互,原理是:JS给OC/Java提供公开的API, 在OC/Java中可以手动调用此API, 并且可以接收OC/Java中传过来的参数,同时可回调OC/Java
ios与JS的交互案例A
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| <!doctype html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0"> <style> body{ background-color:paleturquoise; }
button{ border:0; width: 150px; height: 35px; background-color: orangered; color: white; font-size: 16px; border-radius: 6px; } </style> </head> <body> <h2>JS调用OC中的方法</h2> <button id="btn">访问OC相册</button> <button id="btn1">调用OC提示窗</button> <p></p> </body> <script> // 这段代码是固定的,必须要放到js中 function setupWebViewJavascriptBridge(callback) { if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); } if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); } window.WVJBCallbacks = [callback]; var WVJBIframe = document.createElement('iframe'); WVJBIframe.style.display = 'none'; WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__'; document.documentElement.appendChild(WVJBIframe); setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0) } // 与OC交互的所有JS方法都要在这里注册,才能让OC和JS之间相互调用 setupWebViewJavascriptBridge(function(bridge) {
/* JS给OC提供公开的API, 在OC中可以手动调用此API, 并且可以接收OC中传过来的参数,同时可回调OC */
// 获取用户信息 bridge.registerHandler('getUserInfo', function(data, responseCallback) { console.log("OC中传递过来的参数:", data); // 把处理好的结果返回给OC responseCallback({"userID":"DX001", "userName":"旋之华", "age":"18", "otherName":"旋之华"}) });
// 弹框输出(这里的数据是ios端返回的值) bridge.registerHandler('alertMessage', function(data, responseCallback) { alert(data); });
// 动态跳转到京东商城 bridge.registerHandler('pushToNewWebSite', function(data, responseCallback) { window.location.href = data.url; });
bridge.registerHandler('insertImgToWebPage', function(data, responseCallback) {
var img = document.createElement('img'); img.src = data.url; img.width = 200; document.body.appendChild(img);
});
/* OC给JS提供公开的API, 在JS中可以手动调用此API, 并且可以接收OC中传过来的参数,同时可回调OC */ // 调用OC中的打开相册方法(传数据给ios) document.getElementById('btn').onclick = function () { bridge.callHandler('openCamera', {'count':'10张'}, function responseCallback(responseData) { console.log("OC中返回的参数:", responseData) }); };
document.getElementById('btn1').onclick = function () { bridge.callHandler('showSheet', '', function responseCallback(responseData) { console.log("OC中返回的参数:", responseData) }); }; }) </script> </html>
|
ios与JS的交互案例B
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| //********************** /*苹果设备回调*/ //注册WebViewJavascriptBridge // 这段代码是固定的,必须要放到js中 function setupWebViewJavascriptBridge(callback) { if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); } if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); } window.WVJBCallbacks = [callback]; var WVJBIframe = document.createElement('iframe'); WVJBIframe.style.display = 'none'; WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__'; document.documentElement.appendChild(WVJBIframe); setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0) } function testClick1(code,msg,page) { //JS代码 // 与OC交互的所有JS方法都要在这里注册,才能让OC和JS之间相互调用 setupWebViewJavascriptBridge(function(bridge) { // 弹框输出(获得返回的数据) bridge.registerHandler('alertMessage', function(data, responseCallback) { if(data.code==0){ $('.tip').html('<span class="gr">注册成功,3秒后自动跳转</span>'); $('.register').unbind('click'); setTimeout(function(){ window.location.href='/Login/index.html'; },3000); }else{ $('.tip').html('<span class="re">'+data.tishi+'</span>'); } }); //JS换行 //获得Button /*苹果方法调用传递数据*/(传递数据给ios) //JS代码调用OC"mpTestObjcCallBack"的Handler,并传递参数{'foo': 'bar'} 如果有回调还会显示出来 bridge.callHandler('mpTestObjcCallBack',{code:code,msg:msg,page:page}, function(response) { console.log('JS获得OC返回的结果:', response); // alert(data); }); }); }
|
java与JS的交互案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| var maincode='0', host = window.location.host, mainmsg = {url:'http://'+host,phone:phone,type:2,pwd:pwd,yzm:yzm}, page = 'register';//页面名称 testClick(maincode,mainmsg,page); /*安卓回调app设备(发消息给安卓)*/ function testClick(code,msg,page) { var data = {code:code,msg:msg,page:page}; window.WebViewJavascriptBridge.send( data , function(responseData) { document.getElementById("show").innerHTML = "repsonseData from java, data = " + responseData } ); } //安卓发消息给h5,方法注册 function connectWebViewJavascriptBridge(callback) { if (window.WebViewJavascriptBridge) { callback(WebViewJavascriptBridge) } else { document.addEventListener( 'WebViewJavascriptBridgeReady' , function() { callback(WebViewJavascriptBridge) }, false ); } } connectWebViewJavascriptBridge(function(bridge) { /*安卓方调用方法*/ bridge.registerHandler("functionInJs", function(data, responseCallback) { alert(data); var responseData = "Javascript Says Right back aka!"; responseCallback(responseData); }); })
|