UPDATE: If you’re using Delphi XE7, the TWebBrowser component now includes the method EvaluateJavascript (does the same thing as what my GetJavaScript result does), and applies to both iOS and Android.

In a post on the Embarcadero forums some time ago, Charles Vinal asked about whether it would be possible to have TWebBrowser implement the stringByEvaluatingJavaScriptFromString method which is available in UIWebView, which is inherited from its ancestor, UIView.

The short answer is yes, however it requires some modifications to existing source. You need to make copies of FMX.WebBrowser and FMX.WebBrowser.iOS, and put them in the compiler search path. Next, make the following changes – in FMX.WebBrowser, add this to ICustomBrowser:

[sourcecode language=”delphi”]
function GetJavaScriptResult(const ScriptName: string): string;
[/sourcecode]

Next, add this to TCustomWebBrowser:

[sourcecode language=”delphi”]
function TCustomWebBrowser.GetJavaScriptResult(const ScriptName: string): string;
begin
if Assigned(FWeb) then
Result := FWeb.GetJavaScriptResult(ScriptName);
end;
[/sourcecode]

(make sure you add the function declaration to the class, too). Next, in FMX.WebBrowser.iOS, add this to TiOSWebBrowserService:

[sourcecode language=”delphi”]
function TiOSWebBrowserService.GetJavaScriptResult(const ScriptName: string): string;
begin
if Assigned(FWebView) and FWebView.canGoForward then
Result := FWebView.stringByEvaluatingJavaScriptFromString(NSSTR(ScriptName)).UTF8String;
end;
[/sourcecode]

Note: If your project uses a TTabControl component, you’ll also need to make sure that the source file FMX.TabControl.pas is in your project path.

Now in your project you’ll be able to call the function like this:

[sourcecode language=”delphi”]
var
JSResult: string;
begin
JSResult := WebBrowser1.GetJavaScriptResult(‘SomeScript’);
end;
[/sourcecode]