CHAPTER 4
136
Self-Updating with AIR
One of the features that customers have come to expect from desktop applications is the ability to self-
update. The application, on startup, should look for any updates to the code and then will download and
install them if the user desires. AIR supports this through the Updater class, but there is still some user
interface and networking work to be done to get it all to hang together.
The first thing to do is to define a method where the server can indicate to the AIR application what the
most recent version is and where the download is. To do this, you will use a simple XML file. An example
of this is shown in the following:
<version>
<latest>2</latest>
<download>http://eladelrom.com/temp/SelfUpdatingApp.air</download>
</version>
This file contains two key elements: the version number of the most recent revision and the URL where the
most recent version can be downloaded. In this case, you specify that the most recent version is 1. You
can define your own format for the XML as you please, or use any format you choose for this.
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx=“library://ns.adobe.com/flex/mx”
creationComplete="creationCompleteHandler()">
<fx:Script>
<![CDATA[
import utils.UpdateHandler;
private static const VERSION:Number = 1;
protected function creationCompleteHandler():void
{
var update:UpdateHandler = new UpdateHandler( VERSION,
"http://eladelrom.com/temp/update.xml" );
}
]]>
</fx:Script>
<s:Label text="Version {VERSION}" />
</s:WindowedApplication>
The UpdateHandler Flex class uses this XML file to get the most recent version information and if
necessary download the new code and update the application. The code for UpdateHandler is as follows:
package utils
{
import flash.desktop.Updater;
import flash.events.Event;
import flash.filesystem.*;
import flash.net.*;
import flash.utils.ByteArray;
FLEX AND AIR: TAKING APPLICATIONS TO THE DESKTOP
137
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
public class UpdateHandler
{
private var _version:Number = 0.0;
private var _updateUrl:String = null;
private var _quiet:Boolean = true;
private var _latestVers:Number;
private var _downloadUrl:String;
public function UpdateHandler( version:Number, updateUrl:String, quiet:Boolean =
true )
{
_version = version;
_updateUrl = updateUrl;
_quiet = quiet;
var versReq:HTTPService = new HTTPService();
versReq.addEventListener(ResultEvent.RESULT, onVersionReturn);
versReq.url = updateUrl;
versReq.resultFormat = 'object';
versReq.send();
}
private function onVersionReturn( event:ResultEvent ):void
{
if ( event.result != null && event.result.version != null &&
event.result.version.latest != null )
{
var versionNumber:String = event.result.version.latest;
_latestVers = parseFloat( versionNumber );
if ( _latestVers > _version )
{
_downloadUrl = event.result.version.download;
Alert.show("Download an update to this application now?",
"Application Update",
3, null, onDownloadPromptReturn);
}
else
{
if ( _quiet == false )
mx.controls.Alert.show( 'You are running the most recent
version' );
}
}
}
private function onDownloadPromptReturn(event:CloseEvent):void
{
if ( event.detail == Alert.YES ) {
var codeReq:URLRequest = new URLRequest( _downloadUrl );
var codeStream:URLStream = new URLStream();
codeStream.addEventListener(Event.COMPLETE,onCodeReturn);
codeStream.load( codeReq );
}
}
private function onCodeReturn( event:Event ):void

Get AdvancED Flex 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.