1. 程式人生 > >android ios和js介面互調

android ios和js介面互調

<script type="text/javascript">
var oc;
	var deviceType="${deviceType!}";
	$(function(){
		if(deviceType=="ios"){
			setupWebViewJavascriptBridge(function(bridge) {
				bridge.init(function(message, responseCallback) {
				});
				oc = bridge;    
			});
		}
	});
	
  
	function call(deviceType,id){
		if(deviceType=="ios"){
		  oc.callHandler('openView', {"func":"FUNC_POST_INSURE","id":id}, function(response) {});
		}
		if(deviceType=="android"){
		 window.Js2JavaInterface.openView("FUNC_POST_INSURE",id);   
		}
	}	   
	 
	  
	 //Ios 初始化方法
	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)
	}
</script>
首先進行ios的setupWebViewJavascriptBridge初始化,之後就可以呼叫call傳引數,openview是移動端定義的介面。</span>
注:當android傳遞的引數是json資料時  web端window.Js2JavaInterface.openView(JSON.stringify(json));  客戶端:public void openView(String json)