js和native交互 互相调用

必须这样写,如果用vue可以在app.vue里面写,先把WebViewJavascriptBridge弄到window下面
function setupWebViewJavascriptBridge(callback) {
        if ((window as any).WebViewJavascriptBridge) { return callback((window as any).WebViewJavascriptBridge); }
        if ((window as any).WVJBCallbacks) { return (window as any).WVJBCallbacks.push(callback); }
        (window as any).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)
}
 setupWebViewJavascriptBridge(function (bridge) {
        // window.JSBridge = bridge;
 })
具体用的时候
js调用native(原生的,ios或者安卓端)
(window as any).WebViewJavascriptBridge?.callHandler(native端的方法名, {参数}, (返回值) => {})
 
native调用js的
(window as any).WebViewJavascriptBridge?.registerHandler(native端的方法名, (返回值) => {})
posted @ 2023-06-09 18:19  郭大蛋子  阅读(147)  评论(0编辑  收藏  举报