1、判断终端是安卓端还是IOS端
varu =navigator.userAgent;
varisAndroid = u.indexOf('Android') > -1|| u.indexOf('Adr') > -1;//android终端
varisiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);//ios终端
2、
2-1、ios端回调代码(固定的)
if(isiOS){
/*这段代码是固定的,必须要放到js中*/
functionsetupWebViewJavascriptBridge(callback) {
if(window.WebViewJavascriptBridge) {returncallback(WebViewJavascriptBridge);}
if(window.WVJBCallbacks) {returnwindow.WVJBCallbacks.push(callback);}
window.WVJBCallbacks= [callback];
varWVJBIframe =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) {
document.getElementById('enjoy').onclick =function() {
bridge.callHandler('game',{'data':kuang.innerHTML},functionresponseCallback(responseData) {
// alert("111");
console.log("OC中返回的参数:",responseData)
});
};
})
}
2-2、安卓端回调(enjoy为 HTML DOM对象)
enjoy.onclick =function() {
if(isAndroid){
window.jsObj.HtmlcallJava2(kuang.innerHTML);
}
console.log('触发了分享按钮');
//bridge.callHandler('game', {'blogdURL': 'weidsfdl'},function(response) {})
};