Home  >  

AIR API - Performing Updates in JavaScript

Author photo
AddThis Social Bookmark Button
airLogo-Shadow.jpg

In the previous post, I examined the process for updating AIR applications in ActionScript. This was an extension to my original post about the Updater API inside of Adobe AIR. If you are not familiar with the update functionality inside of AIR, you will probably want to read the introduction.

One of the items that I covered in the introduction was the update process. As a quick review, here are the five steps that I mentioned. These steps are followed by the actual process that will be scripted in JavaScript.

  1. 1. Determine the update version string for your application (the new version you will be updating to)
  2. 2. Download the correct version of that AIR application to local computer (preferably in the application resource directory)
  3. 3. Create an instance of the Updater class
  4. 4. Call the update method of the Updater class
  5. 5. AIR will shut down your AIR application, install the new version, and then restart the application

These steps are very general and apply to both ActionScript and JavaScript. Let's now look at the specific steps in JavaScript needed to update an application.

  • 1. Determine the URL and version string of the application to which you will be updating.
  • 2. Create an instance of the URLStream class and pass it an instance of the URLRequest class (that points the URL from step 1). Since the URLStream.load method is asynchronous, you will need to add an event listener that will listen for then the URLStream is completed.
function getApplicationVersion() {
 	// Get Application Descriptor File
 	var appXML = air.NativeApplication.nativeApplication.applicationDescriptor;
 	// Parse the Application Descriptor File as XML
 	var xmlObject = (new DOMParser()).parseFromString(appXML, "text/xml");
 	// Get the Needed Values from the XML
 	return xmlObject.getElementsByTagName('version')[0].firstChild.nodeValue;
}
  • 3. After the download is complete, you will read the contents of the URLStream into a ByteArray.
function onDownloadComplete( ev ) {
 	var ba = new air.ByteArray();
 	stream.readBytes( ba, 0, stream.bytesAvailable );
 	updateFile = air.File.applicationStorageDirectory.resolvePath("update-" + date.getMilliseconds() + ".air");
 	fileStream = new air.FileStream();
 	fileStream.addEventListener( air.Event.CLOSE, performUpdate );
 	fileStream.openAsync( updateFile, air.FileMode.WRITE );
 	// Write the data to the file
 	fileStream.writeBytes( ba, 0, ba.length );
 	// Close the connection to the file
 	fileStream.close();
}
  • 4. You will now create a new File object in the application storage directory. You will also create a new FileStream object and read the ByteArray into the File object. Since this is an asynchronous process, you will also create an event listener for when this process completes.
function onDownloadComplete( ev ) {
 	var ba = new air.ByteArray();
 	stream.readBytes( ba, 0, stream.bytesAvailable );
 	updateFile = air.File.applicationStorageDirectory.resolvePath("update-" + date.getMilliseconds() + ".air");
 	fileStream = new air.FileStream();
 	fileStream.addEventListener( air.Event.CLOSE, performUpdate );
 	fileStream.openAsync( updateFile, air.FileMode.WRITE );
 	// Write the data to the file
 	fileStream.writeBytes( ba, 0, ba.length );
 	// Close the connection to the file
 	fileStream.close();
}
  • 5. Finally, you will create and instance of the Updater class and pass it the File object and the version you are updating to. AIR takes it from there.
function performUpdate() {
 	var updater = new air.Updater();
 	updater.update( updateFile, updateData.version );
 	air.Introspector.Console.log("Perform Update");
}

The Example Application

While there are many steps to this process, I have provided code and a sample application that you can use to get started. This application allows you to switch between three versions of an application (that are identical except for their version string).



Application Source Code
Download (5 kb)

Additional Resources
Updating AIR Applications
ActionScript basics for JavaScript developers
Working with Byte Arrays

Read more from David Tucker. David Tucker's Atom feed

Comments

Leave a comment


Type the characters you see in the picture above.

Tag Cloud

Poll: Sci-Fi Movies

What's Your Favorite Sci-Fi Movie of All Time?

Vote | View Poll Results | Read Related Blog Entry

Latest Features

  •     Welcome back to the series. This time we are goings to build a really exciting component that will be used to simply display information about the user. Well, you might say why to we need such a component, is there... Continue Reading
  •    Welcome back to our exciting Facebook ActionScript series. In this article we will discuss one of most important (and most exciting) features of the FB platform, it's the publishing of news. We all know when we log in to facebook,... Continue Reading
  • This article provides 10 tips and best practices (in no particular order) for maximizing the benefits that Dojo can bring to your next project. For a more thorough introduction to Dojo, see the article Dojo: The JavaScript Toolkit with... Continue Reading
  •     The notifications are one of the most interesting (and important) parts of the facebook area. In order to completely understand the Flash side of it, we need to understand the basics of the facebook notification, what it is and how... Continue Reading

Development Series

Get an overview of the tools and technologies that work together to allow developers to build Rich Internet Applications (RIAs) quickly and easily.

facebook icon Facebook Application Development

Anatomy of an Enterprise Flex RIA

Recommended for You

@InsideRIA on Twitter

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.