Tar
Tar

Reputation: 9035

Is it possible to receive Objects from the JS side via WebView.addJavascriptInterface?

Can I receive an Object through the WebView.addJavascriptInterface interface ? something like:

public class JavaScriptInterface {
    public void method(WhatEverKindOfType param) {
        Log.i(tag, param.toString());
    }
}

( with: theView.addJavascriptInterface(new JavaScriptInterface(), "obj"); )

and in the JS, for example: obj.method(myEvent);

I obviously tried that, with Object param and String param, but they all come as null. I know I can JSON.stringify it, but this brings that circular object issue (which is solvable, I know, but I don't want to start messing with it)

Is it even possible ?

Upvotes: 1

Views: 152

Answers (1)

Simon MacDonald
Simon MacDonald

Reputation: 23273

First off you should not use addJavascriptInterface if you are using PhoneGap. Please, please write a PhoneGap plugin instead. The reason you want to use our Plugin interface is that we have worked around a number of issues with addJavascriptInterface.

When you use the plugin interface you pass a JSONArray to the Java side. That supports all the basic types like int, String, boolean and of course JSONObject. It is the JSONObject that you can store more structured data.

Upvotes: 1

Related Questions