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:

	function GetJavaScriptResult(const ScriptName: string): string;

Next, add this to TCustomWebBrowser:

function TCustomWebBrowser.GetJavaScriptResult(const ScriptName: string): string;
  if Assigned(FWeb) then
    Result := FWeb.GetJavaScriptResult(ScriptName);

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

function TiOSWebBrowserService.GetJavaScriptResult(const ScriptName: string): string;
  if Assigned(FWebView) and FWebView.canGoForward then
    Result := FWebView.stringByEvaluatingJavaScriptFromString(NSSTR(ScriptName)).UTF8String;

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:

  JSResult: string;
  JSResult := WebBrowser1.GetJavaScriptResult('SomeScript');