Distributing AIR applications involves the following basic aspects:
Creating .air files
Allowing users to install the application
In the following sections, we’ll look at each of these topics in more detail.
AIR application installers are .air files, which contain all the files necessary to run the AIR application. All installers must be digitally signed, which ensures the integrity of the installation file (meaning a user will know the installation file has not been altered since it was published). You can digitally sign an installer using a certificate. Certificates can be of two types: a self-signed certificate or a certificate from a certificate authority (CA) such as VeriSign. When you sign an installer using a self-signed certificate, the installer will display the publisher as unknown. But, if you use a certificate from a CA, the installer will display the identity of the publisher contained in the certificate.
If you want to use a certificate from a CA, you must purchase the certificate. If you want to use a self-signed certificate, you can create the certificate yourself using the Flex SDK, which we’ll look at in the following section.
You can create a self-signed certificate using the Flex SDK. If you are using Flex Builder, you can create a certificate while exporting the installer using the Create Self-Signed Digital Certificate dialog described in the following section. Otherwise, if you’re not using Flex Builder, you’ll need to run a command from the command line.
To create a certificate from the command line, use adt
with the certificate
option. The syntax is as
follows:
adt -certificate -cn certificateName keyType pfxFile password
The certificate name is simply an arbitrary name you assign to
the certificate. For self-signing certificates created using adt
the key type should always be either
1024-RSA or 2048-RSA. Certificates created using adt
are stored in .pfx files, and you must specify the file
to which to save the certificate. Furthermore, certificates must have
passwords. The following line of code creates a certificate:
adt -certificate -cn Example 1024-RSA certificate.pfx r5t6b3
Flex Builder has a wizard that walks you through the steps of creating an .air file for an AIR application. You can select the Project→Export Release Build option from the application menus to open the Export Release Build window. The wizard has three steps:
In this step, you must select the Flex project to export and the name of the .air file to export.
In this step, you must select a certificate to use, and you must specify the password for the certificate. If you don’t have a certificate, you can click the Create button to create a new self-signed certificate.
In this step, you select the files to include in the .air file. Remember that an .air file is the installer, and it must include all the files necessary to run the application. At a minimum, this will require the main .swf file and the descriptor XML file. However, depending on what else your application requires you may need to include additional files.
When you want to create an .air file using the Flex SDK, you can use adt
from
the command line. In this case, you use adt
with the package
option. The syntax is as
follows:
adt -package SIGNING_OPTIONS airFile descriptor FILES_TO_INCLUDE
The signing options vary depending on the type of keystore used
for the certificate, and we’ll see one example of this in a minute.
The output of running adt
in this
way is an .air file, and you must
specify the name of the .air file
to output. To create an .air file
you must specify a descriptor file. You can also specify the files to
include in the installer; those files will always include at least one
file: the main .swf.
The signing options are different based on the types of
keystores used, and the various options are too numerous to list in
detail in this chapter. You can read more about signing options at the
website http://livedocs.adobe.com/flex/3/html/CommandLineTools_5.html.
For the purposes of this chapter, we’ll look at just one specific
example, which is the case of a self-signed certificate created using
adt
. In that case, the signing
options are as follows:
-storetype pkcs12 -keystore pfxFile
The storetype
in this case
tells adt
that the certificate is
of type pkcs12
, and the keystore
tells adt
where to find the file in which the
certificate is stored.
Therefore, the following command will create an .air file called Example.air using a self-signed certificate called certificate.pfx:
adt -package -storetype pkcs12 -keystore certificate.pfx Example.air Example-descriptor.xml Main.swf
Once you run the preceding command, you will be prompted for the password for the certificate.
After you’ve created an .air file, you can distribute it to anyone with the AIR runtime, and he can install the AIR application simply by double-clicking on the .air file. That means one strategy for deploying applications is to email an .air file or make the .air file available for download. However, if the user does not already have the AIR runtime installed, his system will not know what to do with the .air file. Therefore, you may prefer to use the seamless install feature.
The seamless install feature allows users to click a badge on a web page to install an AIR application. A badge is merely a web-based .swf that does the following:
It detects whether the user has the AIR runtime installed.
If the user does not have the AIR runtime installed, he is presented an option to install the runtime from the badge.
Once it is determined that the user has the AIR runtime, the AIR application is installed.
You can create a custom badge .swf file. The steps are as follows:
Create a standard Flex web project.
Use a flash.display.Loader
object to load a .swf file from
http://airdownload.adobe.com/air/browserapi/air.swf,
and load the .swf into ApplicationDomain.currentDomain
.
Use a UI mechanism (such as a button click) to trigger the installation sequence.
Call the installApplication()
method of the content
property of the loader.
The air.swf file on the
Adobe.com server contains a few
methods you can use to create a seamless install badge, the most
important of which is the installApplication()
method. To use this and the other methods on the server you must
load the .swf into the current
application domain. You can then call the methods from the content
property of the loader. The installApplication()
method requires two
parameters: the path to the .air
file and the required AIR runtime version (as a string). Example 21-14 is an example of a badge.
Example 21-14. A basic AIR badge
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="creationCompleteHandler();"> <mx:Script> <![CDATA[ private var _loader:Loader; private function creationCompleteHandler():void { _loader = new Loader(); var context:LoaderContext = new LoaderContext(); context.applicationDomain = ApplicationDomain.currentDomain; _loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler); _loader.load(new URLRequest("http://airdownload.adobe.com/ air/browserapi/air.swf")); } private function initHandler(event:Event):void { button.enabled = true; } private function install():void { (_loader.content as Object).installApplication("Example.air", "1.0"); } ]]> </mx:Script> <mx:Button id="button" label="Install" enabled="false" click="install();" /> </mx:Application>