0

IOS platform supports this kind of feature, but not Android.

Supported types for i-os UIWebView are described under the following URLs

Using JavaScript From Objective-C

Calling Objective-C Methods From JavaScript

Arun Bertil
  • 4,598
  • 4
  • 33
  • 59
X-HuMan
  • 1,488
  • 1
  • 17
  • 37
  • I think what you are looking for is adding a JavaScriptInterface to the WebView, this allows you to get results from JavaScript in your Java code, and you can send data to JavaScript by just loading a script in WebView. Does this help? http://stackoverflow.com/questions/10472839/using-javascript-in-android-webview – anthonycr Apr 21 '15 at 17:32
  • This Javascript interface mentioned by you allows only to transfer primitive data types and Strings Of course String could be used for JSON, but I need the same for complex types. – X-HuMan Apr 21 '15 at 21:40

1 Answers1

2

You can in fact return other Java objects, not only primitive types from methods of injected objects. Consider this simplified example:

Java:

class MyObject {
    class Transport {
        @JavascriptInterface
        public int getField() { ... }
    }

    @JavascriptInterface
    public Object getTransport() { return new Transport(); }
}

webView.addJavascriptInterface(new MyObject(), "myObject");

JavaScript:

{
    ...
    var transport = myObject.getTransport();
    return transport.getField();
}

Java and JavaScript objects live on different heaps, so you have to copy the data over anyways.

Mikhail Naganov
  • 6,643
  • 1
  • 26
  • 26
  • 1
    Thanks for the answer. Would be nice to have a way to transfer byte arrays at least without converting them to String. – X-HuMan Apr 27 '15 at 11:58
  • Sorry, currently arrays can only be passed from JS to Java, not the other way round. Passing as a JSON string and then parsing it on the JS side with `JSON.parse` will probably be the fastest way to pass an array from Java to JS. – Mikhail Naganov Apr 27 '15 at 21:48