The AIR Browser API and User Events

The AIR browser API lets web applications detect, install, and launch AIR applications. There are some restrictions on its use, however:

  • Applications have to opt-in to the detect and launch capability. This is done by specifying <allowsBrowserInvocation> in the application’s descriptor.
  • Installing and launching applications can only be performed in the context of a user event

“In the context of a user event” means that the method is called during an event handler for some event, such as a mouse click, that’s initiated by a user action, such as clicking on a button. Note that this doesn’t mean all mouse events qualify, as they can also be dispatched programmatically.

Why this restriction? To protect the user against malicious (or just really annoying) web pages. WIthout this restriction, pages could install and launch applications as soon as the page is loaded, over and over again, until you give up in disgust and quit using the Internet.

This restriction occasionally trips people up when using the browser API because it’s tempting to wait for a user event, then call getApplicationVersion() to determine whether or not the application is installed, and finally call installApplication() or launchApplication(). But this won’t work because the getApplicationVersion() call is asynchronous; when the specified callback function executes, the code is no longer part of the original user event.

Note that getApplicationVersion(), however, need not be called during a user event. The trick, then, is to call this method right away, when your web page loads. By the time the user clicks on the button, you’ll already know whether you need to install or launch and can do either immediately within the context of the user event.

One final note: consider calling getApplicationVersion() periodically from a Timer. That way, if the application install state does change while the page is open, subsequent clicks on that button will continue to do the right thing.

5 Responses to The AIR Browser API and User Events

  1. Jonathan says:

    Hi, I’ve been using the browser API to launch an AIR application but I noticed that it needs to make calls to an air.swf file hosted by Adobe. My problem is that I need to be able to run it off of a LAN with no internet access. Is this possible? Thanks

  2. Oliver Goldman says:

    No, the browser API doesn’t support offline use. The primary reason for this is that, in some cases, it has to be able to download and install AIR.

  3. firdosh tangri says:

    Is it possible to launch a already installed air application from the browser[Yes, it is. See the documentation for details. —Oliver]

  4. Ryan Watkins says:

    Is it possible to call launchApplication() from javascript in a web page – say a click event on an html element calling a method on a flash object?Or does the event have to be a user event in a visible flash component?I’d like the user to be able to click a DIV, do some logic in javascript and then have it call launchApplication() on a non-visible flash object only if some conditions are met first.It has to be an event in visible Flash content. Otherwise, Flash Player cannot determine if it’s a legitimate user-initiated event. —Oliver

  5. Adrian Raper says:

    I know this is rather a late comment coming more than 2 years after the post!
    I have been trying to load the air.swf from an intranet as I hadn’t read your note that you can’t. The funny thing is that the getStatus() call correctly tells me if I have AIR or not, but the getApplicationVersion always gives null. If I go ahead anyway, installApplication or launchApplication also work.
    I can cope with the actual AIR installation in another way (a distributuable installer) but knowing whether to launch or install the .air app seems such a purely local thing.
    Is there any other way to detect if my AIR application is installed?
    Thanks
    [Not at this time. As always, you can vote features you’d like to see at http://ideas.adobe.com/air. —Oliver]