admin管理员组文章数量:1288020
I need create an application on Delphi XE6 for Android and iOS. This application mest be use a TWebBrowser to show Google Maps, then I need "send" from Delphi to javascript and "receive" mand from javascript to Delphi. In this way I can show a market on map and then know when the user click on marker. I have found this article on web to execute javascript by Delphi code.
But I don't have idea about how can call a delphi procedure from javascript. For example I have this procedure on Delphi:
procedure JSFeekback(aParm1, aParm2, aParm3, aParm4: string);
and I want use Javascript code on TWebBrowser to call it an pass 4 parameters. I have found similar questions but only for Windows application and the answers don't work on Android (I haven't tried on iOS).
I need create an application on Delphi XE6 for Android and iOS. This application mest be use a TWebBrowser to show Google Maps, then I need "send" from Delphi to javascript and "receive" mand from javascript to Delphi. In this way I can show a market on map and then know when the user click on marker. I have found this article on web to execute javascript by Delphi code.
But I don't have idea about how can call a delphi procedure from javascript. For example I have this procedure on Delphi:
procedure JSFeekback(aParm1, aParm2, aParm3, aParm4: string);
and I want use Javascript code on TWebBrowser to call it an pass 4 parameters. I have found similar questions but only for Windows application and the answers don't work on Android (I haven't tried on iOS).
Share Improve this question edited May 2, 2014 at 0:55 Argalatyr 4,6593 gold badges38 silver badges62 bronze badges asked May 1, 2014 at 20:31 MartinMartin 1,0851 gold badge20 silver badges38 bronze badges 1-
Check this link if you have not yet. Although it is done with COM (ergo Windows) it might give you some idea. On the other hand, I also found this link in Google groups, where they speak about how to do it with
TChromium
instead ofTWebBrowser
. Might be worth a look (ifTChromium
can be used for iOS/Android?) – Guillem Vicens Commented May 2, 2014 at 6:52
4 Answers
Reset to default 4The proper way to do it is with the addJavascriptInterface
function of the Android WebView, as can be seen here, here and here.
The Firemonkey TWebBrowser does not expose this function by default.
There is a custom WebView wrapper control in DPF Android as can be seen here.
You could modify that control to add the addJavascriptInterface
function.
For TWebBrowser on IOS you could use the custom web browser controls in sourceforge and here.
Another possible way to do this is with the OnShouldStartLoadWithRequest
event of TWebBrowser. You could do something like:
<script language=”javascript” type=”text/javascript”>
window.location.href=”#param1¶m2¶m3¶m4”;
</script>
or
<script language=”javascript”>
window.navigate(”javascript:thisisatest();”);
</script>
And see if the URL property of OnShouldStartLoadWithRequest
contains #param1¶m2¶m3¶m4
or javascript:thisisatest();
.
Basically the idea is to navigate to a URL that doesn't change the URL of the existing page and pick up that URL in the OnShouldStartLoadWithRequest
event.
With JavaScript it is very mon to access HTTP servers, for example over jQuery. If you can make your Delphi code available over HTTP, either as a local serveror on the web, your JavaScript code can invoke Delphi code by sending a HTTP request, and receive data from Delphi functions as a HTTP response.
Another option are WebSockets. They are an extension of HTTP, work asynchronously so the Delphi server can send data to the script - without having to wait for a request first (known as server push / Comet).
This would be a standards-based, widely used solution, which does not rely on a particular web client. The free open source library Internet Direct (Indy) can be used on all supported platforms to create HTTP servers, stand-alone or integrated with your application.
Other protocols which are availabe for munication between JavaScript and servers might be also worth a look, for example STOMP.
For your specific function, the HTTP call could use a GET request with parameters:
http://localhost/JSFeekback?par1=val1&par2=val2&par3=val3&par4=val4
This is quite an old question, but I have a nice answer : ) We have our own URL scheme registered in our application - thanks to this, we can call a function from the application using the HTML/JS code. All you need to do is register your URL scheme and handle it. Then you can use it:
<a href="yourapp://25/106128?action=card">CLICK</a>
TWebBrowser.URL
can be changed within javascript:
window.location.href=”#param1¶m2¶m3¶m4”;
then run a timer to check TWebBrowser.URL, get the params, call your JSFeekback, reset the URL.
Or no timer, code like this:
wb1.EvaluateJavaScript("window.location.href=”#param1¶m2¶m3¶m4”;");
getParams(wb1.URL);
JSFeekback(p1, p2, p3, p4);
本文标签:
版权声明:本文标题:Callback Delphi function from TWebBrowser by javascript on Delphi XE6 for all platforms (including iOS, ANDROID)? - Stack Overfl 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741174055a2352187.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论