AIR Tip 6 - Launching an Application from the Browser

January 11th, 2008

AIR Tip 6: This tutorial will walk you through the process of launching an AIR application from the browser and passing arguments into it. This tutorial is current for Adobe AIR Beta 3.

AIR Beta 3 introduced the browser API. This API allows for AIR application to be launched from a web browser, and it also allows for arguments to be passed into the application from the browser.

Setting up Your AIR Application

Applications must set the allowBrowserInvocation value to true to be able to receive events from the Browser API (as illustrated in Code Example 1). Once this value is set, your AIR application will receive a BrowserInvokeEvent [Reference] when it is invoked via the Browser API.

XML:
  1. <!-- Whether the application can be launched when the user clicks a link in a web browser.
  2. Optional. Default false. -->
  3. <allowBrowserInvocation>true</allowBrowserInvocation>

Code Example 1 - Application Descriptor File with Browser Invocation

Knowing the API

The Browser API contains a method, launchApplication, that actually invokes your AIR application [more information]. This application can take three arguments (two of which are required).

  1. appID - the application ID of your AIR application
  2. pubID - the publisher ID of your AIR application
  3. arguments - an array of arguments to pass to your AIR application

Getting Your Publisher ID

The publisherID [Reference] will be the same for each application that is created with a given certificate. This ID can be obtained in two ways:

  • In each AIR application the publisherID is shown with the property NativeApplication.nativeApplication.publisherID
  • When an AIR application is installed, the publisherID is saved in the file META-INF/AIR/publisherid.

Creating a Movie to Launch an AIR Application

For a good starting place, you can view the sample install badge that comes with the Adobe AIR SDK. The source for the badge can be found in the src/badge folder. This sample already has the code needed to load the Browser API. This can be seen in Code Example 2.

ACTIONSCRIPT:
  1. ...
  2. _loader = new Loader();
  3. var loaderContext:LoaderContext = new LoaderContext();
  4. loaderContext.applicationDomain = ApplicationDomain.currentDomain;
  5. _loader.contentLoaderInfo.addEventListener(Event.INIT, onInit);
  6.  
  7. try {
  8.     _loader.load(new URLRequest(BROWSERAPI_URL_BASE + "/air.swf"), loaderContext);
  9. } catch (e:Error) {
  10.     root.statusMessage.text = e.message;
  11. }
  12. ...

Code Example 2 - Loading the Browser API (from the Adobe AIR SDK)

The Adobe AIR SDK also contains the code needed to detect if AIR has been installed. We can modify this to display a status message in our custom badge.

 

ACTIONSCRIPT:
  1. private function onInit(e:Event):void {
  2.     _air = e.target.content;
  3.     switch (_air.getStatus()) {
  4.         case "installed" :
  5.             statusMessage.text = "AIR is installed and has been detected."
  6.             stage.addEventListener(MouseEvent.CLICK,onButtonClicked);
  7.             break;
  8.         case "available" :
  9.             // AIR is Available
  10.             statusMessage.text = "AIR is not installed - application cannot be launched."
  11.             break;
  12.         case "unavailable" :
  13.             // AIR Not Available
  14.             statusMessage.text = "AIR is not installed - application cannot be launched."
  15.             break;
  16.     }
  17. }

Code Example 3 - Detecting AIR (Modified from the Adobe AIR SDK)

You could easily extend this example to also install AIR if it isn't installed (as well as your application), but for this tutorial, we will just launch an application that is already installed. Code Example 4 illustrates how to use the installApplication method to actually launch the application.

ACTIONSCRIPT:
  1. private function onButtonClicked(e:Event):void {
  2.        
  3.     statusMessage.text = "Attempting to Launch AIR Application";
  4.     _air.launchApplication(_applicationID,_publisherID,_arguments);
  5.  
  6. }

Code Example 4 - Detecting AIR (Modified from the Adobe AIR SDK)

Inserting the Launch Badge Into a Page

Code Example 5 shows how to insert this badge into a page using SWFObject. All of the values that are passed to the browser API are passed into the badge with the flashvars that are created with the JavaScript SWFObject method so.addVariable.

For this example, I have passed the applicationID and publisherID into the movie with flashvars, but there might be situations where you don't want to expose all of this information. In those cases, you can hard code these values into your movie.

JAVASCRIPT:
  1. var so = new SWFObject("launchMovie.swf", "badge", "250", "75", "9.0.115", "#FFFFFF");
  2. so.addVariable( "applicationID", "net.davidtucker.airtips.BrowserAPITest" );
  3. so.addVariable( "publisherID", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.1" );
  4. so.addVariable( "arguments", "Application Launched from Browser,Argument 2" );
  5. so.write("foo_flashcontent");

Code Example 5 - Inserting the Launch Badge Into a Page with SWFObject

Testing the Example

For the sample to work properly, you will need to have the BrowserInvokeEvent Sample AIR Application from AIR Tip 5. You can install this application here.

Whoa. You need to install Adobe Flash Player.

Exercise Files
Download (15 kb)

Reference
Getting the application and publisher identifiers (Developer Guide)
Checking from a web page if an AIR application is installed (Developer Guide)
Launching an installed AIR application from the browser (Developer Guide)
Loading the air.swf file (Developer Guide)

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

3 comments on “AIR Tip 6 - Launching an Application from the Browser”

  1. 01

    [...] Deploying Adobe AIR applications seamlessly with the badge install feature Launching an Application from the Browser [...]

    Jump to the top of this page
  2. 02

    [...] SOAP Webservice (AIR Beta 1) AIR Tip 5: Passing Arguments to an Application on Install (AIR Beta 3) AIR Tip 6: Launching an Application from the Browser (AIR Beta 3) AIR Tip 7: Using Command Line Arguments (AIR Beta 3) AIR Tip 8: Serializing Objects [...]

    Jump to the top of this page
  3. 03

    [...] 来源:AIR Tip 6: Launching an Application from the Browser [...]

    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