AIR Tip 3 - What Version is My Application

December 14th, 2007

Every AIR application has an Application Descriptor file which is an XML file that defines the properties of the application. This file is where the developer defines the application ID, application version, application name, and many other properties. As of AIR Beta 3 this file is accessible from NativeApplication.nativeApplication.applicationDescriptor in Actionscript and air.NativeApplication.nativeApplication.applicationDescriptor in JavaScript.

VERSION: This tutorial is current for AIR Beta 3.

Actionscript Example

By using this information, we can grab our application descriptor file as below:

Actionscript:
  1. private var airApplicationID:String = "";
  2. private var airApplicationVersion:String = "";
  3. private var airApplicationName:String = "";
  4.  
  5. private function getApplicationInformation():void {
  6.    
  7.     // Get the Application Descriptor File
  8.     var appXML:XML = NativeApplication.nativeApplication.applicationDescriptor;
  9.  
  10.     // Define the Namespace (there is only one by default in the application descriptor file)
  11.     var air:Namespace = appXML.namespaceDeclarations()[0];
  12.    
  13.     // Use E4X To Extract the Needed Information
  14.     this.airApplicationID = appXML.air::id;
  15.     this.airApplicationVersion = appXML.air::version;
  16.     this.airApplicationName = appXML.air::name;
  17.     // Retrieve any additional information from the application descriptor file
  18.    
  19. }

Code Example 1 - Actionscript Example

This example defines three variables: one for the name, application ID, and version of the application. By using E4X, you can retrieve each of these values from the application descriptor file. However, none of these values can be returned in E4X until you tell it which namespace to use. The method namespaceDeclarations returns an array of all of the namespaces in an XML object. Currently, the application descriptor file only has a single namespace. By defining the namespace in this manner (as opposed to hard coding the value) you can ensure that this method will work in future AIR releases as well (unless the API is changed).

JavaScript Example

The Javascript example follows a slightly different approach. The application descriptor file is still retrieved from the NativeApplication class, but JavaScript views the returned value as a string (and not XML). You must first create a DOM Parser object and pass it the XML. Now you can get the values from the XML.

JavaScript:
  1. var airApplication = {};
  2.            
  3. function getVersion() {
  4.                
  5.     // Get Application Descriptor File
  6.     var appXML = air.NativeApplication.nativeApplication.applicationDescriptor;
  7.     // Parse the Application Descriptor File as XML
  8.     var xmlObject = (new DOMParser()).parseFromString(appXML, "text/xml");
  9.     // Get the Needed Values from the XML
  10.     airApplication.version = xmlObject.getElementsByTagName('version')[0].firstChild.nodeValue;
  11.     airApplication.applicationID = xmlObject.getElementsByTagName('id')[0].firstChild.nodeValue;
  12.     airApplication.name = xmlObject.getElementsByTagName('name')[0].firstChild.nodeValue;
  13.                                
  14. }

Code Example 2 - JavaScript Example

The following code samples illustrate how to create an application that retrieves and displays this information. As this is purely an example (and not an actual application), the AIR files are not provided.

Flex Application
Source Code

JavaScript Application
Source Code

AIR | Comments | Trackback | Del.icio.us | Digg | Technorati Jump to the top of this page

6 comments on “AIR Tip 3 - What Version is My Application”

  1. 01

    there is something new:

    var appXml:XML = Shell.shell.applicationDescriptor;

    Adelbert at November 26th, 2007 around 5:33 am
    Jump to the top of this page
  2. 02

    Yes, you are correct. I have not updated these tutorials for Air Beta 2 - because something else is coming soon…

    David Tucker at November 26th, 2007 around 5:53 am
    Jump to the top of this page
  3. 03

    Thanks for publishing this Dave.

    My old code would have failed silently which would have been a bit embarrassing.

    Al at December 15th, 2007 around 9:39 pm
    Jump to the top of this page
  4. 04

    This is exactly what i was looking for. The way i got the version before was NativeApplication.nativeApplication.applicationDescriptor.@version. But since the version property was taken out of the application properties, and made into it’s own tag, this no longer worked from command line sdk, but still worked on flex builder, which i thought was strange. Anyways, thanks so much for publishing this. =)

    David at December 17th, 2007 around 1:23 pm
    Jump to the top of this page
  5. 05

    [...] AIR Tip 3: What Version is My Application (AIR 1.0) [...]

    雨飞blog » David Tucker的air tips at March 5th, 2008 around 8:11 pm
    Jump to the top of this page
  6. 06

    [...] 来源:AIR Tip 3: What Version is My Application [...]

    Jump to the top of this page

Leave a Reply

  •  
  •  
  •  

You can keep track of new comments to this post with the comments feed.

Badges

View David Tucker's profile on LinkedIn
Inside RIA Badge

Community Posts