webapp js与安卓,ios怎么交互

/*这段代码是固定的,必须要放到js中*/
function setupWebViewJavascriptBridge(callback) {

var u = navigator.userAgent;
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
if (!isiOS) {
if (window.WebViewJavascriptBridge) {
callback(WebViewJavascriptBridge)
} else {
document.addEventListener(‘WebViewJavascriptBridgeReady‘, function (event) {
if(window.onWebViewJavascriptBridgeReady) window.onWebViewJavascriptBridgeReady(window.__bridge = WebViewJavascriptBridge);
callback(WebViewJavascriptBridge)
}, false)
}
} else {

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)
}

}

export default {

callhandler (name, data, callback) {
setupWebViewJavascriptBridge(function (bridge) {
bridge.callHandler(name, data, callback)
})
},

registerhandler (name, callback) {
setupWebViewJavascriptBridge(function (bridge) {
bridge.registerHandler(name, function (data, responseCallback) {
callback(data, responseCallback)
})
})
}
}
// 调用
this.$bridge.callhandler(‘shopCode‘,{‘test‘:‘test‘},(data) => {
this.shopCode = data.shopCode;
})

webapp js与安卓,ios怎么交互

上一篇:基于Flink秒级计算时CPU监控图表数据中断问题


下一篇:解决: 移动端经mouseover显示出的弹层中链接点击问题