Tuesday, May 31, 2011

Seamless installation in AIR: Part 3

Today we will learn how to create our own custom badge.

The default AIR installation badge is pretty customizable, which makes it suitable for a lot of cases. But sometimes, you might want to completely redesign your badge and redo it from scratch. This is possible, and we are going to see how to do it right now.

All badges need to load an external .swf file from the Adobe site and work with it, the url of this file is http://airdownload.adobe.com/air/browserapi/air.swf. This file has several Actionscript methods, that the badge needs to function properly.

To call these methods, the loading .swf needs to load the external .swf into the same ApplicationDomain, and to do that we need to create a LoaderContext object, setting the ApplicationDomain.currentDomain as a value of the applicationDomain property of LoaderContext.

When that's done, we can send this LoaderContext object to the load() method of the Loader object that we use for loading the external .swf. We'll add an event listener for this as well.

This all might sound somewhat complicated, but in reality its not. Here's what our code would look like:

myLoader = new Loader();
var myContext:LoaderContext = new LoaderContext();
myContext.applicationDomain = ApplicationDomain.currentDomain;
myLoader.load(new URLRequest("http://airdownload.adobe.com/air/browserapi/air.swf"), myContext);
myLoader.contentLoaderInfo.addEventListener(Event.INIT, onInit);

When the file finishes loading and onInit() function is called, all the methods of this external .swf file are available and we can call them using the content property of the Loader object.

Here's the list of methods that become available:

  • getStatus() - this method returns a string value, which can be "available", "unavailable" or "installed". Available means that AIR runtime can be installed on this OS, unavailable means that it can't be installed, and installed means that it is already installed on the machine.

  • getApplicationVersion() - this returns the version of the AIR application that this badge is meant to install. The method has 3 parameters: application ID, published ID and a link to the function that will be called in return (this method works asynchronously). When the returned function is called - it receives the version of the application as the parameter. If the application is not installed - null is returned.

    The ID of the application is entered when publishing the .air final - in the descriptor file. The publisher ID is the same for all applications under one certificate - you can see what it is using NativeApplication.nativeApplication.publisherID.

  • installApplication() - installs the application. Requires 2 parameters - the URL to the .air file and the required AIR runtime version (as a string value). You can also pass a third parameter to the method, if you want to pass the program that's going to be installed some values.

  • launchApplication() - this launches the AIR application if it is already installed. Needs at least 2 parameters - application ID and published ID. Just like with the previous method, you can pass some values as the third parameter.


Note, that for the AIR application to be able to be launched from a browser, its allowBrowserInvocation element in its descriptor file must be set to true.

That's all for today's tutorial.

Thanks for reading!

Related:

Seamless installation in AIR: Part 1
Seamless installation in AIR: Part 2

No comments:

Post a Comment