Jump to content
RadStudio 10.3.1 was released today Read more... ×
Sign in to follow this  
Rollo62

[Fmx,iOS,Android] TWebBrowser howto handle downloads

Recommended Posts

Hi folks,

I'm trying to use TWebBrowser (embedded in the app) to show a special page in my mobile apps.
These page is able to download images and other stuff from that page.
When I use normal web browser such documents land in the downloads folder.

 

Of coarse mobile apps life in a sandbox, and it seems that downloads don't land in public downloads folder.
iOS seems to open the image in a separate page, which Android seems to do nothing.
From the behaviour so far I can say that the embedded browsers behaves different than the default browser of the system.

My questions are howto handle downloads via embedded TWebBrowser in Android and iOS ?

  • Are downloads blocked completely on mobile ?
  • Are downloads stored somewhere in the sandbox ?
  • Can I configure somewhat  in TWebBrowser (below surface), to finetune behaviour and where documents will land ?
  • Can I hook into the click/ajax events of the TWebBrowser page (if I have no control over the page itself) ?
  • Maybe a concept of control would be to use a local HTML5 wrapper page with frames around the target site,
    so to be able to get control about the inner events of the target site ?
  • I would prefer to use the internal TWebBrowser, if possible, but also 3rd party solutions would be welcome

 

Hope somebody of the mobile experts have found more insights and a good solution already.
I cannot change the HTML5 page, by the way, to add something like a JS-bridge to Delphi directly, so I hope to find some hooks and tweaks around.

 

Rollo

Share this post


Link to post

Can trap and do things with the URL before navigation in OnShouldStartLoadWithRequest (should work correctly in Delphi 10.3 Rio in theory):

https://stackoverflow.com/questions/23416086/callback-delphi-function-from-twebbrowser-by-javascript-on-delphi-xe6-for-all-pl

 

Can modify the settings of the Android WebView here (copy the file to your project and edit) FMX.WebBrowser.Android.pas

 

constructor TAndroidWebBrowserService.Create;
var
  LayoutParams: JRelativeLayout_LayoutParams;
begin
  FWebView := TJWebBrowser.JavaClass.init(TAndroidHelper.Activity);
  FWebView.getSettings.setJavaScriptEnabled(True);
  FListener := TWebBrowserListener.Create(Self);
  FWebView.SetWebViewListener(FListener);
  FFocusChangeListener := TFocusChangeListener.Create(Self);
  FWebView.setOnFocusChangeListener(FFocusChangeListener);
  FWebView.getSettings.setGeolocationEnabled(True);
  FWebView.getSettings.setAppCacheEnabled(True);
  FWebView.getSettings.setDatabaseEnabled(True);
  FWebView.getSettings.setDomStorageEnabled(True);
  FWebView.getSettings.setBuiltInZoomControls(True);
  FWebView.getSettings.setDisplayZoomControls(False);

  FWebViewContainer := TJRelativeLayout.JavaClass.init(TAndroidHelper.Context);
  FChildrenContainer := TJRelativeLayout.JavaClass.init(TAndroidHelper.Context);
  LayoutParams := TJRelativeLayout_LayoutParams.JavaClass.init(TJViewGroup_LayoutParams.JavaClass.MATCH_PARENT, TJViewGroup_LayoutParams.JavaClass.MATCH_PARENT);
  FWebViewContainer.addView(FWebView, LayoutParams);
  LayoutParams := TJRelativeLayout_LayoutParams.JavaClass.init(TJViewGroup_LayoutParams.JavaClass.MATCH_PARENT, TJViewGroup_LayoutParams.JavaClass.MATCH_PARENT);
  FWebViewContainer.addView(FChildrenContainer, LayoutParams);
 
  SetEnableCaching(True);
end;

  • Thanks 1

Share this post


Link to post

Dear Eli,

thank you very much for sharing and finding such nice solution.
I will check later, but that looks good, seems be able to inject something into the browser.

Share this post


Link to post

On IOS the WebView create isn't as clear cut but maybe can be modified in a similar fashion in:

FMX.WebBrowser.Cocoa.pas

Share this post


Link to post

Thanks, I still checking some stuff to get around this at all, with Indy and THttpClient, so far I prefer the new THttpClient.
This may work for 90% of the needs, but may fail to work with a life-video stream via Ajax events, thats why I try to use TWebBrowser
in the first place., since this will work in the native environment with full performance.
But thats another story.


I will check such JS "injections" on Android first, then iOS later, but indeed I had hope to find a general solution to all platforms.

This seems to be a lot of handcafted work, each on Delphi as well as on HTML side.
Maybe there is the simple possibility to send events somehow, like TMessage to be received via TMessageManager) from JS to Delphi ?
 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×