Products
Technologies

Developer resources

Storing ActionScript Objects in the Encrypted Local Store

Avg. Rating 4.8

Problem

You want to store and retrieve an instance of a custom class you have created in the EncryptedLocalStore.

Solution

The solution is to use the ByteArray methods readObject and writeObject, as well as the [RemoteClass] metadata for any classes that will be stored.

Detailed explanation

In this example we will use the EncryptedLocalStore (ELS) API for storing an instance of a 'Contact' class that we have created. This class will have a few fields: first name, last name, and phone number. First we need to prepare our class so that it can be stored in the ELS. We do this by adding the [RemoteClass] metadata before the class definition. For example:

package com.danieldura
{
[RemoteClass(alias="com.danieldura.Contact")]
class Contact
{
    public var firstName:String;
    public var lastName:String;
    public var phoneNumber:String;

    public function Contact( firstName:String, lastName:String, phone:String)
    {
        ...
    }
}
}

Note that the alias is a key that is stored with the class instance and links the class definition with the specific object that is stored in the ByteArray when an instance of that object is serialized. This key can be any unique string identifying this class, but convention is to use the fully normalized package and class name.

The next step is to store an instance of this class in the ELS. The ELS stores only instances of ByteArrays, so first we will have to create an instance of a ByteArray and store the object in it.

var contact:Contact = new Contact( "Daniel", "Dura", "972-555-5555" );
var bytes:ByteArray = new ByteArray();
bytes.writeObject( contact );

Now that we have a ByteArray with the object instance contained within it, we can store this ByteArray in the ELS. The ELS API uses name/value pairs to store data. The key you use as your name is unique to the application. Multiple AIR applications do not share keys or data. The ELS API for storing data is the setItem method.

EncryptedLocalStore.setItem( "contactkey", bytes );

Now that we have stored the data, the only thing left to do is retrieve the data. This uses the ELS API's getItem method. The getItem method only takes a single argument, the key, and returns a ByteArray instance. We then need to retrieve the object out of that ByteArray using the readObject method. One thing to note is that the readObject method only returns Object instances, you will need to cast that Object to the proper class before using. In our case, we will use the 'as' modifier to cast it as a Contact instance.

var bytes:ByteArray = EncryptedLocalStore.getItem( "contactkey" );
var contact:Contact = bytes.readObject() as Contact;
trace( contact.firstName ); // Daniel
trace( contact.lastName ); // Dura
trace( contact.phone ); // 972-555-5555


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes