XE4 Mobile Tip #2 – Loading local HTML content

  

I’ve already answered this on the newsgroups however it deserves a little more attention.
The most important thing to do when deploying additional files with your app is to make sure they are prefixed with “StartUp/”. This tells the deployment manager to deploy these files with the application and place them in the folder specified after the StartUp/ prefix. This prefix is case sensitive.
Here is a screen capture of the deployment manager for the sample project available for download at the end of this post.

Verify file is included without running the app
You can even verify that the files have been deployed with the app by looking in the Applications section of your device in the XCode Organizer.

Sample code
The code below loads the content of the file into the WebBrowser control that is on the form.

unit Unit288;

interface

uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types,
FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.WebBrowser, FMX.StdCtrls, FMX.Layouts;

type
TForm288 = class(TForm)
LoadHtmlButton: TButton;
WebBrowser1: TWebBrowser;
Layout1: TLayout;
procedure LoadHtmlButtonClick(Sender: TObject);
end;

var
Form288: TForm288;

implementation

uses
IOUtils;

{$R *.fmx}

procedure TForm288.LoadHtmlButtonClick(Sender: TObject);
var
LFilename: string;
begin
LFilename := TPath.GetDocumentsPath + ‘/index.html’;
if TFile.Exists(LFilename) then
WebBrowser1.Navigate(‘file://’ + LFilename)
else
MessageDlg(Format(‘File not found: %s’, [LFilename]), TMsgDlgType.mtError, [TMsgDlgBtn.mbClose], 0);
end;

end.

Works on my device!

Download the Code
Download the sample project.
A couple of notes

You wouldn’t deploy a static file to the Documents folder unless you wanted it to be backed up (via iTunes or iCloud) with other user data.
The best location for static files is Library/Application Support/, this is content that is generated when the application runs, or is included with the application.

Comments are closed.