×
Namespaces

Variants
Actions

Archived:Persistent data in FlashLite

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

We do not recommend Flash Lite development on current Nokia devices, and all Flash Lite articles on this wiki have been archived. Flash Lite has been removed from all Nokia Asha and recent Series 40 devices and has limited support on Symbian. Specific information for Nokia Belle is available in Flash Lite on Nokia Browser for Symbian. Specific information for OLD Series 40 and Symbian devices is available in the Flash Lite Developers Library.

Article Metadata
Code ExampleArticle
Created: sangramp@cybage.com (17 May 2010)
Last edited: hamishwillee (14 May 2013)

Contents

Introduction

Shared objects are sometimes referred to as Flash cookies. Shared-objects enable Flash files to save data which persists across multiple sessions. The Shared Objects are used to read and store limited amounts of data on a user's handset. Shared objects store the information in the form of name-value pairs in the device. There are many possible applications for the shared object. For example, a game could store the highest score, a shopping application can keep track of the shopping cart, and certain login related information can also be saved on user’s device.

Difference between Flash and FlashLite Shared Object:

  • Flash supports both Local and Remote shared object, but FlashLite supports only local shared objects.
  • Flash allows sharing of shared object information across different swf files, but FlashLite does not allow access to another file’s shared object.
  • In case of Flash, the modified swf can access the same shared object created by earlier version of file. In case of FlashLite if the file is modified from the original version, it is considered as different swf and can not access the shared object created by earlier version.

Using Shared Object

Check if Device supports Shared Object

The Capabilities class determines the abilities of the mobile device that host SWF file, which lets you customize the content for different devices. There are certain devices which gives access to FlashLite shared objects. Use the following to determine if the device supports shared object

System.capabilities.hasSharedObjects

Returns true if the Flash Lite content can access the Flash Lite version of shared objects. Otherwise, this property returns false.

function fnCheckSharedObjectSupport ()
{
bShdObjSupported = System.capabilities.hasSharedObjects;
}
fnCheckSharedObjectSupport ();

Determining Maximum Shared Object Size Possible

The total amount of storage for Flash Lite shared objects per SWF file is limited by the device to a predetermined size. You can determine this size by using following call.

SharedObject.getMaxSize()

This function call returns numeric value that specifies the total number of bytes the movie is allowed to store on this device.

function fnShdObjLoaded (oShared:SharedObject)
{
txtTotalSize.text = SharedObject.getMaxSize ();
}

Creating Shared Object

Local shared-object can be created by using this static member function:

SharedObject.getLocal(objectName):SharedObject

objectName is the name of the shared-object. If a local shared-object with the specified objectName does not already exist, a new one will be created. The function returns the reference to the newly created shared object.

In case of Flash on desktop computers, the shared object is immediately retrieved but in case of FlashLite, reading and writing the data to a mobile device is relatively slow. So FlashLite offers a listener which is invoked when the device has loaded the shared objects data. So before performing any operations on the shared object make sure that following listener is invoked.

SharedObject.addListener ("ObjectName",fnShdObjLoaded);
var oShdObj = SharedObject.getLocal ("ObjectName");
function fnShdObjLoaded (oShared:SharedObject)
{
if (oShared.getSize () == 0)
{
//Initialize the shared object here.
}
else
{
//Access the shared object data properties here.
}
}

Writing To Shared Object

The shared object stores the data in the form of name-value pair. A shared-object has a class member named data, which is an object. To write to a shared-object, add members to data and assign the value to it.

sharedObjecInstance.data.propertyName = propertyValue;

The shared object is written to device automatically when-ever we close the application. Whatever the latest data we have assigned to the shared object is written when the FlashLite application is closed. If required, we can also force FlashLite to immediately write the data to device using the flush() call.

sharedObjecInstance.flush()

Unlike in Flash Player, the write operation is asynchronous and hence the result is not available immediately. If the flush operation is successful, it returns 'true' and if it is unsuccessful, it returns 'false'. In many cases, FlashLitePlayer returns 'pending', meaning that flush operation will be completed asynchronously.

var sFlushResult = oShdObj.flush ();
switch (sFlushResult)
{
case 'pending' :
_txtStatus.text = "Flush Request Pending";
break;
case true :
txtStatus.text = "Data is flushed successfully.";
break;
case false :
txtStatus.text = "Flush failed. Data was not flushed.";
break;
}

Reading Shared Object

As shared object stores the data in the form of name-value pair, it’s easy to read the data. Just access the property of data member and you get the value of that property.

propertyValue = sharedObjecInstance.data.propertyName;

As the data is stored as the object, you can use the for-in loop to get all the properties and their values.

for (var Prop in sharedObjecInstance.data) {
trace( Prop +" : " + sharedObjecInstance.data[Prop] );
}
function fnShdObjLoaded (oShared:SharedObject)
{
txtCurrSize.text = oShared.getSize ();
txtTotalSize.text = SharedObject.getMaxSize ();
if (oShared.getSize () == 0)
{
trace ("This is the first time");
txtOutput.text = "You are using the Application first time";
}
else
{
txtOutput.text = "Welcome Back :: " + oShared.data.name + "\nYour Last Session was on:\n " + oShared.data.ltLog;
}
}
SharedObject.addListener ("ObjectName",fnShdObjLoaded);
oShdObj = SharedObject.getLocal ("ObjectName");

Deleting the Shared Object

We can delete the shared object by simply calling the clear method of shared object.

sharedObjecInstance.clear();

Executing this call purges all the data from the shared object and deletes the shared object.

Demo Example

The Demo Example will show, how to use the Shared Objects in FlashLite Applications.

Whenever you run the application first time on device, you will get the following status message 'You are using the Application first time.' The application will also display maximum shard object size supported on target device.

Once you enter your name and click 'StoreData', the name and login time is saved on mobile device.

Next time you run the application, it will display the login name you saved, in addition to the time when you logged in, last time. The time will be shown in standard format.

SharedObject.JPG

Add the following lines of code to make the data persistent in FlashLite.

var bShdObjSupported:Boolean = false;
var oShdObj:SharedObject;
 
 
function fnCheckSharedObjectSupport ()
{
bShdObjSupported = System.capabilities.hasSharedObjects;
}
function fnCreateSharedObject ()
{
SharedObject.addListener ("Prefs",fnShdObjLoaded);
oShdObj.onStatus = function (infoObject:Object)
{
for (var prop in infoObject)
{
_root.txtStatus.text = _root.txtStatus.text + prop + "::----::" + infoObject[prop];
}
};
oShdObj = SharedObject.getLocal ("Prefs");
}
function fnShdObjLoaded (oShared:SharedObject)
{
txtCurrSize.text = oShared.getSize ();
txtTotalSize.text = SharedObject.getMaxSize ();
if (oShared.getSize () == 0)
{
txtOutput.text = "You are using the Application first time";
var dNow:Date = new Date ();
oShared.data.ltLog = String (dNow);
oShared.data.name = "FlashLiteSharedObject";
}
else
{
txtOutput.text = "Welcome Back :: " + oShared.data.name + "\nYour Last Session was on:\n " + oShared.data.ltLog;
}
}
function fnStoreData ()
{
if (txtInput.text != '')
{
var dNow:Date = new Date ();
oShdObj.data.ltLog = String (dNow);
oShdObj.data.name = txtInput.text;
var sFlushResult = oShdObj.flush ();
switch (sFlushResult)
{
case 'pending' :
_root.txtStatus.text = "Flush Request Pending";
break;
case true :
_root.txtStatus.text = "Data is flushed successfully.";
break;
case false :
_root.txtStatus.text = "Flush failed. Data was not flushed.";
break;
}
}
}
 
fscommand2 ("DisableKeypadCompatibilityMode");
status = fscommand2 ("FullScreen", true);
_focusrect = false;
_root._quality = "HIGH";
Stage.scaleMode = "noScale";
 
 
fnCheckSharedObjectSupport ();
if (bShdObjSupported)
{
fnCreateSharedObject ();
}

Download

You can download the example at Persistent Data in FlashLite. This is tested on Nokia 5800 XpressMusic device.

This page was last modified on 14 May 2013, at 06:17.
52 page views in the last 30 days.
×