美文网首页
h5与原生的交互,JSBridge

h5与原生的交互,JSBridge

作者: 流泪手心_521 | 来源:发表于2021-03-30 20:38 被阅读0次

    1.新建一个文件

    
    // import Vue from 'vue';
    export const setupWebViewJavascriptBridge = callback => {
        var u = navigator.userAgent;
        var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
        if (!isiOS) {
            if (window.WebViewJavascriptBridge) {
                // eslint-disable-next-line no-undef
                callback(WebViewJavascriptBridge);
            } else {
                document.addEventListener(
                    'WebViewJavascriptBridgeReady',
                    function () {
                        // eslint-disable-next-line no-undef
                        callback(WebViewJavascriptBridge);
                    },
                    false
                );
            }
        } else {
            if (window.WebViewJavascriptBridge) {
                return callback(window.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 const  appUtil={
        //网点管家下载附件
        downLoadFn(params){
            setupWebViewJavascriptBridge(function(bridge){
                bridge.callHandler('downLoadFn',params)
            })
        }
    }
    
    

    2.在那个页面中使用,就在那个页面中引入,并且调用改方法

    import { appUtil } from "../assets/JSBridge/JSBridge.js";
    appUtil.downLoadFn(params)//调用原生方法
    

    或者下载的时候拿到url的地址http://xxx,让安卓监听一下Webview监听下载的回调,然后让手机浏览器下载就行
    安卓的代码

    webView.setDownloadListener(new DownloadListener() {
                @Override
                public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.addCategory(Intent.CATEGORY_BROWSABLE);
                    intent.setData(Uri.parse(url));
                    startActivity(intent);
                }
            });
    
    

    相关文章

      网友评论

          本文标题:h5与原生的交互,JSBridge

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