在webView 中使用JS 調用 Android / IOS的函數 Function

來源:互聯網
上載者:User

標籤:

最近做一個項目,混合了NativeCode 和 HTML,為了便於JS 調用App的一些方法,統一封裝一個Js方法,記錄如下

Android 端首先要再WebView中允許JS的調用

WebView myWebView = (WebView) findViewById(R.id.webview);WebSettings webSettings = myWebView.getSettings();webSettings.setJavaScriptEnabled(true);myWebView.addJavascriptInterface(new WebAppInterface(this), "JsAndroid");

 

IOS端使用的是一個開源庫 EasyJsWebView,在IOS端引用即可

 

JS代碼:

function callApp(method) {        var args = [].slice.call(arguments).splice(1);        var s = "";        if (/android/i.test(navigator.userAgent)) {//安卓            s = window["JsAndroid"][method].apply(window.JsAndroid, args);        }        if (/ipad|iphone|mac/i.test(navigator.userAgent)) {//ios            s = window["JsIOS"][method].apply(this, args);        }        return s;    }//與IOS互動的方法window.EasyJS = {    __callbacks: {},        invokeCallback: function (cbId, removeAfterExecute) {        var args = Array.prototype.slice.call(arguments).splice(2);        for (var i = 0, l = args.length; i < l; i++) {            args[i] = decodeURIComponent(args[i]);        }        var cb = EasyJS.__callbacks[cbId];        if (removeAfterExecute) {            EasyJS.__callbacks[cbId] = undefined;        }        return cb.apply(null, args);    },    call: function (obj, functionName, args) {        var formattedArgs = [];        for (var i = 0, l = args.length; i < l; i++) {            if (typeof args[i] == "function") {                formattedArgs.push("f");                var cbId = "__cb" + (+new Date);                EasyJS.__callbacks[cbId] = args[i];                formattedArgs.push(cbId);            } else {                formattedArgs.push("s");                formattedArgs.push(encodeURIComponent(args[i]));            }        }        var argStr = (formattedArgs.length > 0 ? ":" + encodeURIComponent(formattedArgs.join(":")) : "");        var iframe = document.createElement("IFRAME");        iframe.setAttribute("src", "easy-js:" + obj + ":" + encodeURIComponent(functionName) + argStr);        document.documentElement.appendChild(iframe);        iframe.parentNode.removeChild(iframe);        iframe = null;        var ret = EasyJS.retValue;        EasyJS.retValue = undefined;        if (ret) {            return decodeURIComponent(ret);        }    },    inject: function (obj, methods) {        alert(obj);        window[obj] = {};        var jsObj = window[obj];        for (var i = 0, l = methods.length; i < l; i++) {            (function () {                var method = methods[i];                var jsMethod = method.replace(new RegExp(":", "g"), "");                jsObj[jsMethod] = function () {                    alert("qq");                    return EasyJS.call(obj, method, Array.prototype.slice.call(arguments));                };            })();        }    }};

  

說明一下,一開始調用Android也是採用window["JsAndroid"][method].apply(this,args),這樣的話,就完全一致了。但是在調試的時候發現這種方式無法正常調用,google後發現是由於this的的影響域導致的,需要指明尋找域。 參考

 

記錄一下!

 

在webView 中使用JS 調用 Android / IOS的函數 Function

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.