WebViewJavascriptBridge实现js与android和ios原生交互
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WebViewJavascriptBridge实现js与android和ios原生交互相关的知识,希望对你有一定的参考价值。
1、实现原生与js交互
<!-- 申明交互 这段代码固定必须有 --> 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); }); }); });
<div> <a id = "call">跳转手机端 > </a> </div>
参考:https://www.jianshu.com/p/e37ccf32cb5b
以上是关于WebViewJavascriptBridge实现js与android和ios原生交互的主要内容,如果未能解决你的问题,请参考以下文章
WebViewJavascriptBridge实现js与android和ios原生交互
Android混合开发之WebViewJavascriptBridge实现JS与java安全交互
Android 利用WebViewJavascriptBridge 实现js和java的交互