Behrooz
Tahanzadeh


Add update functionality
to an AIR application

One of the greatest features of Adobe AIR platform is its update capability. You can embed update functionality in every AIR application so easy and be sure that all of you users will run latest and bug free version of you application. The great thing about this is that it’s free to developer up to include this functionality in anyway that they like.

To accomplish an update process, generaly these steps must be done:

1.Check for update to see whether a newer version is available or not.

2.Download update file from server, if update is necessary.

3.Write Downloaded file in temporary location on users hard drive.

4.Call update function of Updater class and AIR will handle the rest of update process.


step #1:

In this step we need a file on server in a given URL that save data about update file version and location:

[btcode title=’xml’ lang=’xml’]
<?xml version=’1.0′?>
<info>
<version> 1.01 </version>
<location> http://app.behrooz.tahanzadeh.com/sample-app/sample-file.air </location>
</info>
[/btcode]

Then we need a function to get this data (in this example it’s checkForUpdate). First this function send a request, and wait for its response. Then compare update file version with installed version. If these two value are different then we go to next step (we call startUpdate function).

[btcode title=’AS3′ lang=’java’]
var updateURL:String=””;
var updateVersion:String=””;

function checkForUpdate():void
{
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE , urlloaderOnComplete);
urlLoader.load(new URLRequest(“http://app.behrooz.tahanzadeh.com/sample-app/app-info.xml”));
}

function urlloaderOnComplete(event:Event):void
{
var xml:XML = new XML(URLLoader(event.target).data);
updateURL = xml.location; updateVersion = xml.version;
//check version status
var appXML:XML = NativeApplication.nativeApplication.applicationDescriptor;
var air:Namespace = appXML.namespaceDeclarations()[0];
var version:String = appXML.air::versionNumber;
if(version != updateVersion)
{
startUpdate();
}
}
[/btcode]
step #2 & #3:

In continuing of update process we download update file that actually exist some where on a server. we get this file from URL that we got it in previuos step. Then we write this file on user hard drive.

[btcode title=’AS3′ lang=’java’]
var urlStream:URLStream;
var fileData:ByteArray;
var updateFile:File;

function startUpdate():void
{
urlStream = new URLStream(); fileData = new ByteArray();
urlStream.addEventListener(Event.COMPLETE , urlStreamOnComplete);
urlStream.load(new URLRequest(updateURL));
}

function urlStreamOnComplete(event:Event):void
{
urlStream.readBytes(fileData,0,urlStream.bytesAvailable);
updateFile = File.applicationStorageDirectory.resolvePath(“update.air”);
var fileStream:FileStream = new FileStream();
fileStream.addEventListener(Event.CLOSE , fileStreamOnClose)
fileStream.openAsync(updateFile, FileMode.WRITE);
fileStream.writeBytes(fileData,0,fileData.bytesAvailable);
fileStream.close();
}

function fileStreamOnClose(event:Event):void
{
performUpdate();
}
[/btcode]
step #4:

Now every thing is ready to perform update. We have update file and its version, so the last thing is calling update function.

[btcode title=’AS3′ lang=’java’]
function performUpdate():void
{
var updater:Updater = new Updater();
updater.update(updateFile, updateVersion);
}
[/btcode]

And now AIR window will pop up and install new updated version of your application and then your application will launch again.