Thursday, June 2, 2011

Updating AIR applications: Part 2

Today we will start making an application that checks for updates automatically and has the option to update.

To do that, you need to have some space on a web server. You'll need it to store the .air file, and a text file at. The text file will be the one that's going to be checked by our application, and if the version value in the text file is greater than the application's current version, it will ask the user to update.

The text file will hold not only the verison value, but also the link to the new version of the application. For example:

http://www.mysite.com/applications/myapp_version2.air,2.0

Where the first part contains the link, and the second contains the version number (2.0 in the example).

Put the .air file on a web server that we can reach to, and put this text file there as well. Call it something like lastversion.txt.

After that, create a new AIR project. We'll call it ExampleV1, where V1 stands for "Version one". Open your descriptor file for this application (if you are using Flash IDE - go to Publish, you'll be able to set values there) and set the application version value to 1.0. Create a self-signed certificate, set an application ID and remember these values - you'll use them later in the next version of the application. Or don't remember them, and just duplicate the project file when you create version 2 and the settings will be the same.

Anyway, our application needs to have a textfield in it and a button (button component) in it. Call them myText and myButton. You can set the prompt for the button to be "Update".

Let's write some code (this won't be the whole code yet) that will read the lastversion.txt file we have on our server and check if we need to update.

First of all we read the data and split it to values:

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];
}

Now we need to compare the version in the text file to our current version. To get the current version, we need to access the descriptor XML and get it from there, like this:

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

So, if we update our code, it would look like this:

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;
}
}

Now, if the user is running an outdated version of the application, his Update button becomes available and he is informed that an update can be downloaded.

That's all for today.

Thank you for reading!

Related:

Updating AIR applications: Part 1
Updating AIR applications: Part 3

No comments:

Post a Comment