美文网首页
WebViewJavascriptBridge js和原生交互

WebViewJavascriptBridge js和原生交互

作者: 赠前端 | 来源:发表于2018-02-24 09:15 被阅读0次
    function setupWebViewJavascriptBridge(callback) {  
        if (window.WebViewJavascriptBridge) {  
            return callback(WebViewJavascriptBridge);  
        }else{  
            document.addEventListener(  
                    'WebViewJavascriptBridgeReady'  
                    , function() {  
                        callback(WebViewJavascriptBridge)  
                    },  
                    false  
            );  
            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)  
        }  
    }  
      
    setupWebViewJavascriptBridge(function(bridge) {  
        try{ //此处try catch注意  android 有init初始化方法,必须调用 不调用后续注册的事件将无效 而IOS却没有init方法 调用会报错 所以得捕获异常  
            bridge.init(function(message, responseCallback) {  
                console.log('JS got a message', message);  
                var data = {  
                    'Javascript Responds': '测试中文!'  
                };  
                console.log('JS responding with', data);  
                responseCallback(data);  
            });  
        }catch(e){  
      
        }  
      
        bridge.registerHandler('orderResultJsHandler', function(data, responseCallback) {  
            //处理回调  
        });  
      
    });
    

    相关文章

      网友评论

          本文标题:WebViewJavascriptBridge js和原生交互

          本文链接:https://www.haomeiwen.com/subject/xynxxftx.html