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:
-
private var airApplicationID:String = "";
-
private var airApplicationVersion:String = "";
-
private var airApplicationName:String = "";
-
-
private function getApplicationInformation():void {
-
-
// Get the Application Descriptor File
-
var appXML:XML = NativeApplication.nativeApplication.applicationDescriptor;
-
-
// Define the Namespace (there is only one by default in the application descriptor file)
-
var air:Namespace = appXML.namespaceDeclarations()[0];
-
-
// Use E4X To Extract the Needed Information
-
this.airApplicationID = appXML.air::id;
-
this.airApplicationVersion = appXML.air::version;
-
this.airApplicationName = appXML.air::name;
-
// Retrieve any additional information from the application descriptor file
-
-
}
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.
-
var airApplication = {};
-
-
function getVersion() {
-
-
// 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
-
airApplication.version = xmlObject.getElementsByTagName('version')[0].firstChild.nodeValue;
-
airApplication.applicationID = xmlObject.getElementsByTagName('id')[0].firstChild.nodeValue;
-
airApplication.name = xmlObject.getElementsByTagName('name')[0].firstChild.nodeValue;
-
-
}
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




6 comments on “AIR Tip 3 - What Version is My Application”
01
there is something new:
var appXml:XML = Shell.shell.applicationDescriptor;
02
Yes, you are correct. I have not updated these tutorials for Air Beta 2 - because something else is coming soon…
03
Thanks for publishing this Dave.
My old code would have failed silently which would have been a bit embarrassing.
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. =)
05
[...] AIR Tip 3: What Version is My Application (AIR 1.0) [...]
06
[...] 来源:AIR Tip 3: What Version is My Application [...]
Leave a Reply