H5 呼叫 Android 和 iOS 方法

kzh4435發表於2019-01-16
function share(){
    var u = navigator.userAgent;
    if(u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {
            //安卓手機
            android.andshare(title,content,url)
    }else if(u.indexOf('iPhone') > -1) {
            setupWebViewJavascriptBridge(function(bridge) {
               bridge.callHandler('iosshare', {'title':title,'content':content,'url':url}, function responseCallback(responseData) {
                   console.log("JS received response:", responseData);
                     })
             })
    }
}

function setupWebViewJavascriptBridge(callback) {
        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 = 'https://__bridge_loaded__';
        document.documentElement.appendChild(WVJBIframe);
        setTimeout(function() {
            document.documentElement.removeChild(WVJBIframe)
        }, 0)
    }
本作品採用《CC 協議》,轉載必須註明作者和本文連結
持續做一件事,必定有所成就

相關文章