最近做一个项目,混合了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的的影响域导致的,需要指明查找域。 参考

记录一下!

更多相关文章

  1. 栈处理问题
  2. android2.1短信应用
  3. Android(安卓)Native层Binder.transact()函数调用 Binder.onTran
  4. 由浅入深全面剖析ThreadLocal
  5. Android(安卓)Webkit中DocumentWriter模块分析
  6. Android游戏引擎《Rokon》:常见问题汇总(2010.11.15更新)
  7. DexClassLoader 实现 Android(安卓)插件加载
  8. Android布局文件的加载过程分析:Activity.setContentView()源码
  9. android SQLite 优化(三)使用事务 优化 insert

随机推荐

  1. as随笔
  2. Android给第三方应用添加系统签名的两种
  3. 自定义Android六边形进度条(附源码)
  4. 简单实用 layoutAnimation
  5. selector in Android
  6. adb devices 找不到设备的解决方法
  7. [ubuntu 10.04 64bit][android]编译时报
  8. android:动态申请权限(一)
  9. android 循环滚动实现
  10. 关闭Android开机锁屏