Home/Code tips, Patches, Uncategorized/Adding Javascript processing capability to TWebBrowser in iOS

Adding Javascript processing capability to TWebBrowser in iOS

Views:
216

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;
begin
  if Assigned(FWeb) then
    Result := FWeb.GetJavaScriptResult(ScriptName);
end;

(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;
begin
  if Assigned(FWebView) and FWebView.canGoForward then
    Result := FWebView.stringByEvaluatingJavaScriptFromString(NSSTR(ScriptName)).UTF8String;
end;

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:

var
  JSResult: string;
begin
  JSResult := WebBrowser1.GetJavaScriptResult('SomeScript');
end;
By | 2014-10-19T14:32:06+00:00 November 6, 2013 10:04 am|Code tips, Patches, Uncategorized|13 Comments

About the Author:

13 Comments

  1. Szatmari Jozsef December 2, 2013 at 9:29 pm - Reply

    Hi,
    There is same solution in Android?

    Thanks

    • admin December 3, 2013 at 12:09 am - Reply

      It may be a similar solution; the evaluateJavascript method of the WebView class has been left out of the JWebView interface in Androidapi.JNI.Webkit, so that file would need to be modified to include the method. The help on this method is here. I’m not familiar with how to invoke methods that use the JValueCallback interface; I may be able to look into this later.

  2. ilnar March 20, 2014 at 10:40 pm - Reply

    hi..

    Realties who could figure out how to perform JS in android?

    • admin March 21, 2014 at 8:58 am - Reply

      I’ll have a look into that if I have time. I’ve been rather busy with other things..

  3. ilnar March 27, 2014 at 6:25 pm - Reply

    Or maybe even from a variable to load html?

    and maintaining the file reading takes time

  4. Thierry May 17, 2014 at 7:43 am - Reply

    Hi , i have a problem.
    I modify the FMX.WebBrowser file but aftr this how do you recomplie this component ?

    Another question : This modification is compatible with Android ?

    • admin June 4, 2014 at 7:55 am - Reply

      It’s not compatible with Android as yet, and you don’t need to recompile the component: just include the altered units in your project directory.

  5. Ricardo Craveiro September 10, 2014 at 9:40 pm - Reply

    XE 6 – What am I doing wrong?
    [DCC Fatal Error] FMX.WebBrowser.pas(452): F2051 Unit FMX.Platform.iOS was compiled with a different version of FMX.WebBrowser.IFMXWBService

    • admin October 14, 2014 at 6:34 pm - Reply

      I suspect you need to copy FMX.WebBrowser.pas to your project directory, and modify it as per the instructions in the article.

  6. victor April 18, 2015 at 7:09 pm - Reply

    any idea for ask for internet conection. if the mobile dont have internet conection cant enter some area in app!!!??? Embarcadero xe7 multi device

  7. mmparody June 3, 2017 at 3:28 pm - Reply

    How TWebBrowser permissions are given to use camera and audio?

  8. cp September 13, 2017 at 10:12 pm - Reply

    where are the file FM.Webbrowser to edit ?

    • Delphi Worlds September 14, 2017 at 11:46 am - Reply

      FMX.WebBrowser.pas is in the C:\Program Files (x86)\Embarcadero\Studio\xx.x\Source\fmx folder, where xx.x is the version number

Leave a Reply

Show Buttons
Hide Buttons