View comments | RSS feed

SharedObject


Object
    |
    +-SharedObject

public dynamic class SharedObject
extends Object

The SharedObject class is used to read and store limited amounts of data on a user's computer. Shared objects offer real-time data sharing between objects that are persistent on the user's computer. Local shared objects are similar to browser cookies.

Here are three possible uses of shared objects:

Local shared objects maintain local persistence. For example, you can call SharedObject.getLocal() to create a shared object that contains the high score in a game. Because the shared object is locally persistent, Flash saves its data attributes on the user's computer when the game is closed. The next time the game is opened, the high score from the previous session is displayed. Alternatively, you could set the shared object's properties to null before the game is closed. The next time the SWF file runs, the game opens without the previous high score.

To create a local shared object, use the following syntax:

var so:SharedObject = SharedObject.getLocal("userHighScore");
so.data.highScore = new Number();
so.flush();

In the example, the shared object is explicitly flushed, or written to a disk. When an application closes, shared objects are automatically flushed; however, it is shown here to demonstrate the step of writing data to a disk.

Local disk space considerations: Local shared objects can be very useful, but they have some limitations that are important to consider as you design your application. Sometimes your SWF files may not be allowed to write local shared objects, and sometimes the data stored in local shared objects can be deleted without your knowledge. Flash Player users can manage the disk space that is available to individual domains or to all domains. When users lower the amount of disk space available, some local shared objects may be deleted. Flash Player users also have privacy controls that can prevent third-party domains (domains other than the domain in the current browser address bar) from reading or writing local shared objects.

Note: Local content can always write third-party shared objects to disk, even if writing of shared objects to disk by third-party domains is disallowed.

Macromedia recommends that you check for failures that are related to the amount of disk space available and to user privacy controls. Perform these checks when you call getLocal() and flush():

SharedObject.getLocal() -- This method returns null when the user has disabled third-party shared objects and the domain of your SWF file does not match the domain in the browser address bar.

SharedObject.flush() -- This method returns false when the user has disabled shared objects for your domain or for all domains. It returns "pending" when additional storage space is needed and the user must interactively decide whether to allow an increase.

If your SWF file attempts to create or modify local shared objects, make sure that your SWF file is at least 215 pixels wide and at least 138 pixels high (the minimum dimensions for displaying the dialog box that prompts users to increase their local shared object storage limit). If your SWF file is smaller than these dimensions and an increase in the storage limit is required, SharedObject.flush() fails, returning "pending" but then calling your SharedObject.onStatus handler with a result of "SharedObject.Flush.Failed".

Availability: ActionScript 1.0; Flash Player 6

See also

getLocal (SharedObject.getLocal method), flush (SharedObject.flush method), onStatus (SharedObject.onStatus handler)

Property summary

Modifiers

Property

Description

 

data:Object

The collection of attributes assigned to the data property of the object; these attributes can be shared and/or stored.

Properties inherited from class Object

constructor, __proto__, prototype, __resolve


Event summary

Event

Description

onStatus = function(infoObject:Object) {}

Invoked every time an error, warning, or informational note is posted for a shared object.

Method summary

Modifiers

Signature

Description

 

clear() : Void

Purges all the data from the shared object and deletes the shared object from the disk.

 

flush([minDiskSpace:Number]) : Object

Immediately writes a locally persistent shared object to a local file.

static

getLocal(name:String, [localPath:String], [secure:Boolean]) : SharedObject

Returns a reference to a locally persistent shared object that is available only to the current client.

 

getSize() : Number

Gets the current size of the shared object, in bytes.

Methods inherited from class Object

addProperty, hasOwnProperty, isPropertyEnumerable, isPrototypeOf, registerClass, toString, unwatch, valueOf, watch



Version 8

Comments


Equan said on Sep 18, 2005 at 7:19 AM :
How come SharedObject only works in flashplayer 6, and not in 7, or 8?
MM tech writer said on Sep 28, 2005 at 4:30 PM :
The Availability section lists the earliest version of Flash Player that supports the API. The SharedObject class is supported by Flash Player 6 and higher.
marci1202 said on Mar 31, 2006 at 11:12 PM :
Equan had some problems with the "local_so.fla" but got it to work..could someone tell me how he got it to work? I've been having the same problems with it. I tried this piece of code but the flashcookie didn't seem to be stored because everytime I reload the page, the value of the object seems to always be null.
var myLocal_SO:sharedObject = sharedobject.getLocal("flashcookie" );
if (myLocal_SO.data.name == null)
{
myLocal_SO.data.name = "alrdylogdin";
myLocal_SO.flush();
// show loading
gotonextframe();
}
Thanks in advance.
swartz1999 said on Jul 5, 2006 at 4:54 PM :
marci1202, your code works if you use the correct name for the class, which is SharedObject (not sharedObject or sharedobject). Also, the correct name for the global function is nextFrame(), not gotonextframe().
No screen name said on Aug 30, 2006 at 3:39 PM :
I've tried this exact code in Flash 8 and the "bookmarking" doesn't work, it doesn't even write a file.

// Get the kookie
var my_so:SharedObject = SharedObject.getLocal("kookie");

// Get the user of the kookie and go to the frame number saved for this user.
if (my_so.data.user != undefined) {
this.user = my_so.data.user;
this.gotoAndStop(my_so.data.frame);
}

On every frame put this code:

// On each frame, call the rememberme function to save the frame number.
function rememberme() {
my_so.data.frame=this._currentframe;
my_so.data.user="John";
}
No screen name said on Feb 12, 2007 at 9:53 AM :
'sharedobject' should be 'SharedObject'

ur code should be:

var myLocal_SO:SharedObject = SharedObject.getLocal("flashcookie", "/");
function SO() {
myLocal_SO.data.name = "Already logged..";
myLocal_SO.flush();
if (!myLocal_SO.data.name) {
this.nextFrame();

}
}
SO();
abhikrish said on Feb 27, 2007 at 3:33 AM :
My file which uses the shared object feature of flash MX is not working in flash 7 or 8. Is there nething else for shared objects on higher versions?
SilverDev said on Mar 20, 2007 at 8:02 AM :
It's worth noting here that the data SharedObject is not truly overwritten, but changes are merged. This merge process can occasionally have errors. For myself, I'm storing an object into the shared object. I would change a property of this object and flush. The file date on the sol file would not update and my change would not be there the next time I loaded the shared object.

After working with the file for a while I came up with the following solution on every change:
Set my variable to undefined, flush the shared object.
Set my variable to the new object, flush again.

The difficult part here was that while looking solely at the code, flush was returning true. It would be nice if there was a status code that revealed the amount of data that changed, in some way. Using getSize doesn't really work since I was changing a property set to a string to another string of the same length.
xbrotherx said on Jul 3, 2007 at 10:01 AM :
I can store a Shared Object on my local machine without being confronted by the Flash Player Settings dialog box asking persmission when addressing an IPv4 address.

But when addressing the same site using an IPv6 address I get the Flash Player settings dialog asking persmission to store the Shared Object and the address displayed in the warning message is URL-encoded.

Why does it ask for permission on IPv6 but not IPv4?

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/8/main/00002661.html