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?
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?
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 = myIterface.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 = myIterface.getData();");
For iPhone you can call a JavaScript function from Objective-C by using stringByEvaluatingJavaScriptFromString.
For instance, to get the value of an input field named "fname":
NSString * fString =[webView stringByEvaluatingJavaScriptFromString:@"document.getElementByName(\"fname\").value"];
NSLog(@"fString: %@",fString);