pengwei1024 / JsBridge

A simpler, extendable bidirectional communication Frame between Android WebView and Javascript
https://github.com/pengwei1024/JsBridge/wiki
Apache License 2.0
643 stars 88 forks source link

Is it possible pass Native class object to js ? #14

Closed mksantoki closed 5 years ago

mksantoki commented 6 years ago

I added description here below link

https://stackoverflow.com/questions/51481575/web-apps-in-webview-android

pengwei1024 commented 6 years ago

@mksantoki Sorry,Currently not support returning object to js。

I thought about how to support this situation by modify the code, I briefly talk about the idea.

Step1

@JSBridgeMethod
    public JBMap testReturnObject() {
        WritableJBMap jbMap = new WritableJBMap.Create();
        jbMap.putString("data", "abc");
        jbMap.putCallback("toast", new JBCallback() {
            @Override
            public void apply(Object... args) {

            }
        });
        return jbMap;
    }

Note: WritableJBMap not support JBCallback convert to JS function now

Step2

modify com/apkfuns/jsbridge/JBUtilMethodFactory.java#CallMethod, parse r.msg to js Object and return

buffer.append("var r = _callJava(id, moduleName, methodName, args);");
buffer.append("if (r && r.success){");
buffer.append("if(ret)return r.msg");

When I am free, I will think deeply about the concrete implementation.