与本地环境的Webkit通信 [英] Webkit communication with native environment

查看:86
本文介绍了与本地环境的Webkit通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个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中使用<一个href="http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface%28java.lang.Object,%20java.lang.String%29"相对=nofollow> 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();");

这篇关于与本地环境的Webkit通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆