Shared Objects in Flex

It will be value add to your web applications if they can recognize the users who are revisiting your applications and displays customized messages to the user. Adobe Flex provides a good solution for this. Adobe Flex allows developers to store objects on the client’s machine without compromising security on the client’s machine.

What are Shared Objects

Shared objects behave like cookies. You use SharedObject class to store data on the client’s hard disk and retrieve those objects in the same session or in another session. Applications can access their own shared object data only. You can make your shared data object available to other application from the same domain and you cannot share with applications from other domains.

Shared objects allow you to write simple objects like Arrays, String and Date. You can create multiple shared objects for one application. Each shared object is associated with a name. To add data to shared objects, you use the data property of the shared object. Below is a function, which display a welcome message if the user is revisiting.

By default, Flash can save locally persistent SharedObject objects of up to 100 KB per domain. When the application tries to save data to a shared object that would make it bigger than 100 KB, Flash Player displays the Local Storage dialog box, which lets the user allow or deny local storage for the domain that is requesting access.

1 private function storeVisitDate():void

2 {

3 var visitDate:SharedObject = SharedObject.getLocal(“userVisitedDate”);

4 if(visitDate.data.lastVisitedDate != null)

5 {

6 Alert.show(“Welcome back, you visited last on ” + visitDate.data.lastVisitedDate);

7 }

8 visitDate.data.lastVisitedDate = new Date();

9 visitDate.flush();

10 }

Explanation:

Line #3: getLocal() method of the shared object retrieves shared object with the name specified. If the object does not exist it is created.

Line #4: checking if lastVisitedDate property is available. If it is available then we retrieve it.

Line#8 and #9: here we reset the lastVisitedDate property and use flush() method to save the shared object. Invoking flush() method is not necessary, shared objects are automatically saved when the application is closed.

Please refer to SharedObject in Flex language reference for more details.

10 Responses to Shared Objects in Flex

  1. Hi there.
    I was thinking that today with the capacities of the HDDs out there maybe Adobe should have to reconsider the default size of the shared object before the user see the notification window. Since a lot of users may not have any idea about what is a shared object and especially if they think that it is “another” cookie, they may be scared and not allow more space. I know S.O. are small but there are so many GBs out there that I don’t think a few more KBs or even MBs for shared objects could cause a problem. In fact flash player could occupy that space during installation so no worries..

  2. Sujit Reddy G says:

    Hi,

    Users can configure Flash player Local storage options. They can increase or decrease the default values.
    I agree that 100 KB is less. But i think 100 KB is set as the default value keeping in mind that the shared objects will usually not occupy more than 100 KB and more over it is the client who has to take a decision on how much space the applications should be allowed to use.
    If the applications require more than 100 KB, then what they can do is that, when they are storing the shared objects for the first time you can specify how much space your application will be occupying in the flush() method. The warning message displayed to the users is not that scary🙂 it just says that the domain requires more space and allows users to allow or deny the request.
    Please check this link for more details. http://www.macromedia.com/support/documentation/en/flashplayer/help/help06.html

    I will try to find out from the Flash team on why the limit was set to 100 KB.🙂

  3. Yes I know about the settings, I wrote that comment because I’ve seen the face of some users who just browsing some sites and when they saw the dialog box they were ready to quit the browser!🙂
    If you find out why 100kbs default I would like to know(just by curiosity).
    Thanks anyway.

  4. venu reddy says:

    hi sujith nice example… can i use this same example in air application??because now i need to develop one application in air there we need to maintain information about the users.can u help me please

  5. Hi Venu,

    Yes, Shared Objects work with AIR also. Since this is AIR application, please consider using SQL Lite database packaged with AIR.

    Hope this helps.

  6. celine says:

    Hi,

    I need to create a folder or a file with flex application and save this file in my machine, Can I do this with SharedObjects?
    Can you give me an example?

    Thank you

  7. Hi Celine,

    No you cannot do that on the machine where the Flex application is running on Flash player. Please try creating an AIR application.

    Hope this helps.

  8. Rodrigo B. says:

    Sujit,

    I can store a ArrayCollection in SharedObjects? or i can store a variable multiple times with a object of ArrayCollection.

    ex:

    for (var currentObj:Object in myArrayCollection){
    _sharedobject.data.variable = currentObj;
    }
    _sharedobject.flush();’

  9. Hi Rodrigo,

    You can store any AS3 data type in Shared Object.

    Hope this helps.

  10. Srinivasarao says:

    Hi i am trying to get Httpsession session object from jsp to Swf file.I am passing swf file through wrapper javascript method from jsp.

    How can get session attribute value in my flex application??

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: