JS和安卓 IOS的交互 例子式记录

(function () {
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/);

if(isAndroid){
(function(){
function android_inter(){
let t_url = "xxxxxx";
document.getElementById("事件监听的DOM节点").onclick = function () {
android.onNavigateByScheme(t_url);
}
}
android_inter();
})();
}else if(isiOS){
/**
* UIWebView与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 = 'https://__bridge_loaded__';
document.documentElement.appendChild(WVJBIframe);
setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
}
let t_url = "xxxxxx";
document.getElementById("事件监听的DOM节点").onclick = function () {
setupWebViewJavascriptBridge(function(bridge) {
bridge.callHandler("客户端定义好的函数名",t_url, function (responseData) {
});
});
}
}
})();
posted @ 2018-08-30 16:20  惊沙男孩  阅读(404)  评论(0)    收藏  举报