与本地环境的Webkit通信通信、环境、Webkit

2023-09-06 05:59:36 作者:青衫烟雨客

如果我有一个WebKit的观点装在我的Andr​​oid或iPhone应用程序,我可以在WebKit中传递数据来回从页面?

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?

例如,我可以拉一个HTML的Web视图下来,在我的存储设备上查看数据填充表格?

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?

推荐答案

是在Android中使用 addJavaScriptInterface()。

Yes in Android using addJavaScriptInterface().

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

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

和在你的web视图的HTML。

And in the html in your webview.

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

顺便说一句,如果您使用的网页是不是你(所以你不能修改它),你可以的插入的JavaScript的内联。例如:

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();");