Friday, June 3, 2011

Updating AIR applications: Part 3

Today we continue making our updatable application.

Now that our code can check the current version of the running application and compare it to the latest version, which it finds in the lastversion.txt file on a webserver, we need to add some functionality to the Update button.

The first thing we need to do when the user hits the Update button, is download the new .air file. This is done using an URLStream object.

myButton.addEventListener(MouseEvent.CLICK, pressUpdate);

function pressUpdate(MouseEvent):void{
myText.text = "Downloading update.";
var stream:URLStream = new URLStream();
stream.addEventListener(ProgressEvent.PROGRESS, downloadProgress);
stream.addEventListener(Event.COMPLETE, downloadComplete);
stream.load(new URLRequest(airUrl));
}

When the download is in progress, we want to show the user how much is left to download:

function downloadProgress(evt:ProgressEvent):void{
myText.text = "Downloading update: " + evt.bytesLoaded + "/" + evt.bytesTotal + " bytes."
}

When the download is complete, we'll want to save the file locally and then feed it to the Update object we'll create:

function downloadComplete(evt:Event):void{
myText.text="Download complete!";
var file:File = File.applicationStorageDirectory.resolvePath("newAppVersion.air");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
var bytes:ByteArray = new ByteArray();
evt.target.readBytes(bytes);
fileStream.writeBytes(bytes);
fileStream.close();
var myUpdater:Updater = new Updater();
myUpdater.update(file, lastVersion);
}

The full code:

var lastVersion:String;
var airUrl:String;

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("http://www.mysite.com/lastversion.txt"));

function onComplete(evt:Event):void{
lastVersion = evt.target.data.split(",")[1];
airUrl = evt.target.data.split(",")[0];

var descriptor:XML = NativeApplication.nativeApplication.applicationDescriptor;
var ns:Namespace = descriptor.namespaceDeclarations()[0];
var currentVersion:String = descriptor.ns::version;

if(currentVersion == lastVersion){
myText.text="You are running the latest version of the application.";
myButton.enabled = false;
}else{
myText.text="You're running an older version of the application (" + currentVersion + "). A new version is available (" + lastVersion + ").";
myButton.enabled = true;
}
}

myButton.addEventListener(MouseEvent.CLICK, pressUpdate);

function pressUpdate(MouseEvent):void{
myText.text = "Downloading update.";
var stream:URLStream = new URLStream();
stream.addEventListener(ProgressEvent.PROGRESS, downloadProgress);
stream.addEventListener(Event.COMPLETE, downloadComplete);
stream.load(new URLRequest(airUrl));
}

function downloadProgress(evt:ProgressEvent):void{
myText.text = "Downloading update: " + evt.bytesLoaded + "/" + evt.bytesTotal + " bytes."
}

function downloadComplete(evt:Event):void{
myText.text="Download complete!";
var file:File = File.applicationStorageDirectory.resolvePath("newAppVersion.air");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
var bytes:ByteArray = new ByteArray();
evt.target.readBytes(bytes);
fileStream.writeBytes(bytes);
fileStream.close();
var myUpdater:Updater = new Updater();
myUpdater.update(file, lastVersion);
}

Now, create a new project with the same descriptor informations, except for the version - change it to 2.0. After that you can upload this to your web server, so that the link in the lastversion.txt text file refers to this new .air file.

And you're done!

Thanks for reading!

Related:

Updating AIR applications: Part 1
Updating AIR applications: Part 2

1 comment:

Post a Comment