Local storage, session storage and global storage
Modern browsers like Apple Safari, Mozilla Firefox and Google Chrome supports application centric storage, meaning that you can store data in the browser cache. There are 3 variations you can choose from: local storage, session storage and global storage. In many ways the API resembles a lightweight variation of TRegistry under Delphi or an INI file section.
Local storage
When used, the browser creates a persistent isolated storage file (a cabinet file) that is private for your application alone (the scope of what “your application” is – is defined by the domain and URI). No other website can access this data.
Session storage
If you use a session storage object the browser creates, much like local storage, a private storage file for your application. But this file is temporary and is deleted from the file-system when the browser is closed or the user navigates somewhere else.
Global storage
Global storage creates a storage space that can be accessed by any other application. This is used by websites like facebook and youtube (and most other big labels). The global data is in turn often picked up by third party websites which can then use the login information to identify you.
Using storage from Smart Mobile studio
Smart supports all 3 types of storage (and more) mechanisms and have isolated these classes in the unit w3storage. The classes all derive from a common ancestor making it a snap to change storage medium as you move along.
Unless you want to isolate the storage object in a custom unit, the most logical place to initialize the object is in your TApplication object. Under Delphi TApplication was quite anonymous, but under Smart this is slightly different and TApplication really is the central hub of your projects.
procedure TApplication.ApplicationStarting; var mForm: TW3CustomForm; begin FStorage := TW3LocalStorage.Create; FStorage.Open('TestApp'); //Add code above this line mForm := TForm1.Create(Display.View); mForm.Name := 'Form1'; RegisterFormInstance(mForm,True); inherited; end;
Reading and writing information
Once the storage object is initialized and made public as part of your TApplication instance, reading information is a piece of cake:
procedure TForm1.InitializeObject; begin inherited; FEdit := TW3EditBox.Create(self); FEdit.onChanged := HandleEditChanged; try FEdit.Text := TApplication(Application).Storage.getKeyStr('myText'); except on e: exception do; end; end;
Writing information is likewise simple:
procedure TForm1.HandleEditChanged(sender:TObject); begin TApplication(Application).Storage.setKeyStr('myText',FEdit.Text); end;
As you probably notice, the storage medium is string only. This is not something imposed by us – but the way the javascript API is defined. You can store as many key-value pairs as you like (with a limit of roughly 10 megabytes).