Webkit communication with native environment

android, iphone, webkit

Solution

Yes in Android using addJavaScriptInterface().

class MyInterface {
    private String someData;
    public String getData() { return someData; }
}

webview.addJavaScriptInterface(new MyInterface(), "myInterface");

And in the html in your webview.

<script type="text/javascript">
     var someData = myInterface.getData();
</script>

By the way, if the webpage you are using is not yours (so that you can't modify it), you can insert javascript inline. For instance:

webview.loadUrl("javascript:document.getElementById('someTextField').value = myInterface.getData();");

Problem

If I have a webkit view loaded in my Android or iPhone app, can I pass data back and forth from the page in webkit? For example, can I pull an html view down from the web and populate a form in that view with data stored on my device?

Original source