2009-07-14

Using SharedObject across SWFs

At work we have a web application that is comprised of various SWF files written using Flex 3. Just this past week I was trying to share some data amongst two of those SWFs and was wondering why SWF B could not read the ShareObject created by SWF A.

See normally to read/create a SharedObject you write code like this:

var so:SharedObject = SharedObject.getLocal( "mySO" );

The above line of code will read the SharedObject called mySO or create one if it does not already exist in a similar folder structure as the one that follows (varies based on OS - I am using Vista):

C:\Users\Jimmy\AppData\Roaming\Macromedia\Flash Player\#SharedObjects\blabla\localhost\SWFA\mySO.sol

Since the SharedObject is created in a folder specific to SWF A (see the "\localhost\SWFA" above), SWF B, will not be able to read this SharedObject, thus the both applications cannot share data between eachother.

In order to solve this, you must specify an additional parameter to the getLocal() method call to tell it to save the SharedObject in a folder location accessible by both applications. An example is:

var so:SharedObject = SharedObject.getLocal( "mySO", "/" );

What this will do now is create the SharedObject in the following folder structure:

C:\Users\Jimmy\AppData\Roaming\Macromedia\Flash Player\#SharedObjects\blabla\localhost\mySO.sol

Hope this is helpful to some when working with SharedObjects. It is a simple, yet primitive way to share data amongst two Flex applications without requiring the use of a server of any kind.