1. 判断当前页面的环境
var u = navigator.userAgent; var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端 var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端 if(isAndroid){ //android环境 JSAndroid.getToken()
}else if(isiOS){ //ios环境 getToken()
}
2.由于ios内部问题 有时要做特殊处理
web端:
/*这段代码是固定的,必须要放到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方法都要放在此处注册,才能调用通过JS调用OC或者让OC调用这里的JS*/ setupWebViewJavascriptBridge(function(bridge) { /**OC调用JS代码含参数*/ bridge.registerHandler('getToken', function(data) { console.log(data); }) })
ios端代码参照:https://www.cnblogs.com/chaoyuehedy/p/8583272.html
来源:https://www.cnblogs.com/yuzihong/p/10220180.html