WebViewJavascriptBridge实现js与android和ios原生交互
1、实现原生与js交互
成都创新互联自2013年起,先为永新等服务建站,永新等地企业,进行企业商务咨询服务。为永新企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
function setupWebViewJavascriptBridge(callback) { //android使用 if (window.WebViewJavascriptBridge) { callback(window.WebViewJavascriptBridge) } else { document.addEventListener( 'WebViewJavascriptBridgeReady' , function() { callback(window.WebViewJavascriptBridge) }, false ); } //ios使用 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 = 'https://__bridge_loaded__'; document.documentElement.appendChild(WVJBIframe); setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0) } setupWebViewJavascriptBridge(function(bridge) { //oc或android掉js bridge.registerHandler('result', function(data, responseCallback) { if(data.errorCode == null || data.errorCode == "") window.location.href = "refresh.html?result=success"; else window.location.href = "refresh.html?result=fail"; }); //js掉原生 $(document).on('click', '#call', function() { bridge.callHandler('call', {'sessionId':$.cookie("sessionId")}, function(response) { //处理oc过来的回调 var responseData = { 'Javascript Says':'Right back atcha!' }; responseCallback(responseData); }); }); });
参考:https://www.jianshu.com/p/e37ccf32cb5b
分享题目:WebViewJavascriptBridge实现js与android和ios原生交互
浏览地址:http://scyanting.com/article/ihhgde.html