webapp js与安卓,ios怎么交互

Posted momo-倩倩

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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怎么交互的主要内容,如果未能解决你的问题,请参考以下文章

iOS的webview和JavaScript的交互方法,求个小Demo,

H5与原生APP交互方式 (IOS及安卓)

autojs正经的交互-安卓与webview

安卓Webview 与 vue h5 使用js交互

H5 与Native的交互方案

iOS WKWebView JS 与 原生交互小结