26 September 2011
You also need:
- A Facebook account: Sign up
- A registered Facebook application: See Part 1
- (optional) A local or remote web server for implementing badge installers and adding update capabilities to the application.
Beginning
Note: This tutorial series was updated September 2011 to use the Adobe ActionScript 3 SDK for Facebook Platform, which uses OAuth 2.0. Facebook is requiring all applications to use OAuth 2.0 by October 1, 2011.
This tutorial series steps you through the process of building and deploying either a desktop or web application that retrieves, displays, and posts Facebook data. In Part 1, you built a local Flex and Facebook desktop application with Flash Builder and the Adobe ActionScript 3 SDK for Facebook Platform. In Part 2, you package and deploy this application as an AIR desktop application. In the process you will:
Note: Facebook frequently changes to the site's user interface and API. This makes it difficult to keep this tutorial 100% up-to-date. Please be sure to add and read the comments at the end of this tutorial for any user interface or API modifications that might have occurred.
Start by modifying some of the application properties and then packaging it as an AIR file.
Open Flash Builder.
If you did not complete Part 1 of the tutorial series, you need to do the following:
Change the application ID used by the AIR runtime to uniquely identity the application and specify an application icon.
Every AIR application contains an application descriptor file which is used by the AIR runtime when installing and launching the application. It contains information about the application (like its name, id, description, file icons, and so on) and information about the initial operating system window to load the application SWF into when the application is launched (its size, position, and so on). See the AIR documentation for more details about the application descriptor file.
<id>com.adobe.samples.FacebookUserStatusAIR</id>
<icon>
<image16x16>images/fb_status_app_icon16.png</image16x16>
<image32x32>images/fb_status_app_icon32.png</image32x32>
<image48x48>images/fb_status_app_icon48.png</image48x48>
<image128x128>images/fb_status_app_icon128.png</image128x128>
</icon>
Next, use the Flash Builder ADT (AIR Developer Tool) to package and sign the desktop application.
You cannot package and distribute an AIR application withouta digital signature. The digital signature has two main purposes: to providesignificant assurance to your users that you are a trusted signer whose codewill not do anything malicious to their computers and that the application theyare installing has not been accidentally or maliciously altered since it waspackaged.
AIR code-signing certificates are sold by a number ofcompanies, including ChosenSecurity, GlobalSign, Thawte, and Verisign;certificates are typically only sold to registered companies and the processincludes submittal and verification of multiple company documents. When youinstall an AIR application, the name of the publisher associated with a trustedcertificate is displayed in the installation dialog box. If you create your owncertificate as you are doing here, the publisher name is displayed as Unknown.In general, you should never install applications that have a publisher ofunknown (although many people do). Creating your own certificate is useful fordeveloping applications. As well, AIR still uses this self-signed certificate toverify that the AIR package has not been altered since it was signed. Refer tothe AIR documentation for more information about digitallysigning AIR applications.
One way to distribute your application is to simply distribute this AIR file you just created. In order to install the application, however, the user must have the AIR runtime already installed and have the proper version needed to install this application. You are going to install the application using the AIR file right now as it is the simplest install process, but you will learn a better way to distribute the application using native installers and installation badges in the following sections.
To make sure the user has the right version of the AIR runtime installed before installing the application, you can create native or badge installers. Both check to make sure the correct version of the AIR runtime needed to run the application is installed before installing the application. If it is not, the required version of the AIR runtime is first downloaded and installed. In this section, you make a native installer to install from the desktop. In the next, you make a badge installer to install from a web page.
To create a native installer, you use the same ADT tool in Flash Builder that you just used to create the AIR file. You can only create a native installer for the operating system you are using. To create native installers for other operating systems, you need to install and use Flash Builder on each of those other operating systems.
If the user is online and does not have the required version of the AIR runtime installed, she will get a dialog box to grant permission to install the runtime (see Figure 12).
If the user is offline, the AIR runtime cannot be downloaded so the user will instead get a message that she needs to install Adobe AIR before she can install the application (see Figure 13).
You can create seamless installation experiences for users by creating and distributing native installers or by creating and deploying installation badges on web pages. Adobe provides an HTML template page for creating badge installers. All you need to do is edit this file and then deploy your badge along with the AIR file it references on a website. This badge allows for an image to be loaded into the button and for the color of the badge and the button to be changed.
First, use an existing installation badge to see how it works.
To create your own badge installer, the first thing you need to do to is to put your AIR file on a web server so you can specify a URL to download it from in the badge installer. You will place additional files on the web server in later steps.
Next, modify the HTML template page for creating the badge installer.
The default_badge.html file is a template file for creating badge installers. You make changes to this file and then post it on your server or more typically, copy its HTML code and place it inside a web page on your website. The default_badge.html file references the badge.swf file and the AC_RunActiveContent.js files.
<title>Facebook User Status Application Installer Page</title>
// Major version of Flash required var requiredMajorVersion = 10;
// Minor version of Flash required var requiredMinorVersion = 2;
// Minor version of Flash required var requiredRevision = 0;
'flashvars','appname=Facebook%20User%20Status&appurl=http://stallons.com/facebook/userstatusAIR/FacebookUserStatusAIR.air&airversion=2.6&imageurl=fb_status_app_icon128.png',
The appname is the name of the application that will be displayed in a message under the install button if the AIR runtime is not present.
The appurl is the URL of the AIR file on the server. For appurl, use the URL for the location of your AIR file on your web server. You must use an absolute, not relative, URL.
The airversion is the version of the AIR runtime required. Your application is using AIR 2. You can find this value in the FacebookUserStatusAIR-app.xml file.
The imageurl is the URL of an image to display in the badge interface. For imageurl, you can specify a relative or absolute URL. Use the fb_status_app_icon128.png file in the project's images folder. In the code example above, a relative URL is specified and you just have to post this image file in the same directory as the HTML page.
You can also specify some additional, optional variables. You can set buttoncolor to a six-digit hex value (or to transparent) to set the color of the button background and messagecolor to a six-digit hex value for setting color of the text message displayed under install button.
document.write('<table id="AIRDownloadMessageTable"><tr><td>Download <a href="http://stallons.com/facebook/userstatusAIR/FacebookUserStatusAIR.air">
Facebook User Status</a> now.<br /><br />
Download <a href="http://stallons.com/facebook/userstatusAIR/FacebookUserStatusAIR.air">Facebook
User Status</a> now.<br /><br />
You are now ready to test the badge installation.
Because you already installed the application, you get the option to run, replace, or uninstall the application (see Figure 18).
Before you distribute a desktop application, you should build into it the ability to be updated in the future. The application needs a way to check and see if it should be updated and then have the ability to update itself (using the AIR runtime). In order to accomplish this, you need to add some logic to your application and put an XML file on your web server that has details for the latest version and location of the application. Your application checks this XML file periodically (whever you decide it should) to see if it needs be updated.
You add update logic to your application using an update framework created by Adobe that includes APIs and optional UI components for the update process. It provides the ability to periodically check for updates based on an interval or at the request of the user and to download AIR files (the updates) from a web server. It also provides information and messages to the user throughout the update process. You are going to use the default user interface provided with the framework in your application, but this interface can also be customized.
You are going to create two configuration files. One will live on your web server and contains update information for your application including the latest version number and the URL for the corresponding AIR file. The other is a local file, part of the application itself, and contains information about how the update should occur including how often and where to retrieve info about the latest application version (the location of the first file on the web server).
<?xml version="1.0" encoding="utf-8"?>
<update xmlns="http://ns.adobe.com/air/framework/update/description/2.5">
<versionNumber>2.0.0</versionNumber>
<url>http://stallons.com/facebook/userstatusAIR/FacebookUserStatusAIR_v2.air</url>
<description>This is the latest version of the Facebook User Status application.</description>
</update>
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="http://ns.adobe.com/air/framework/update/configuration/1.0">
<url>http://stallons.com/facebook/userstatusAIR/update.xml</url>
<delay>1</delay>
</configuration>
The delay element sets the interval in days between periodic checks of new updates. The default value is 0 and means no periodic checks are done.
This config file can also contain a defaultUI element to control which dialog boxes are displayed during the update process. See the AIR documentation for more details.
Now you need to add code to your application to use the update framework to check the update config file on the server to see if it needs to be updated.
The applicationUpdater.swc includes the basic update functionality without any user interface. The applicationUpdater_ui.swc includes the basic functionality of the update library but also includes a user interface for displaying update options. You are going to use the API of the ApplicationUpdaterUI class (air.update.ApplicationUpdaterUI) to initialize the update process. For more details on using this class, peruse its API in the ActionScript 3 Reference.
_appUpdater that is an instance of the ApplicationUpdaterUI class (air.update.ApplicationUpdaterUI). Be sure to choose ApplicationUpdaterUI from code-hinting so the import statement is written for you.import air.update.ApplicationUpdaterUI;
private var _appUpdater:ApplicationUpdaterUI = new ApplicationUpdaterUI();
windowedapplication1_creationCompleteHandler() function, set the ApplicationUpdaterUI configurationFile property to the config file you created earlier in the data folder, update_config.xml. _appUpdater.configurationFile = new File("app:/update_config.xml");
The File class (flash.filesystem.File) is only available in desktop applications and an instance of it represents a directory or file on the user's computer. The app:/ refers to the application directory of the application, where the application was stored when it was installed. See the File class API in the ActionScript 3 Reference for more details.
windowedapplication1_creationCompleteHandler() function, call the initialize() method of the ApplicationUpdaterUI class. This method initializes the update framework and if the delay that was specified in the update_config.xml file has expired, it starts the update process. Otherwise it restarts the timer (which is managed by the AIR runtime)._appUpdater.initialize();
windowedapplication1_creationCompleteHandler() function, register to listen for the initialized event of the ApplicationUpdaterUI class (which is broadcast after the initialization is complete) and specify a handler updateInitializedHandler .You register to listen for events in ActionScript using the addEventListener() function. You pass to it the name of the event you want to listen for (like click or initialized in this case) and a reference to the function that will be the event handler. Instead of using the string "initialized" as the name of the event, use the static constant of the UpdateEvent class equal to this string.
You can place the code before or after the initialize() call; code execution in Flash Player is single-threaded and the handler will never be invoked before all the code inside this function is executed. Be sure to select UpdateEvent form code-hinting so the import is written for you.
import air.update.events.UpdateEvent;
protected function initApp():void
{
FacebookDesktop.init("YOUR APPLICATION ID HERE",loginHandler);
_appUpdater.configurationFile = new File("app:/update_config.xml");
_appUpdater.initialize();
_appUpdater.addEventListener(UpdateEvent.INITIALIZED, updateInitializedHandler);
}
updateInitializedHandler() that receives one argument of type UpdateEvent and returns nothing.import air.update.events.UpdateEvent;
protected function updateInitializedHandler(event:UpdateEvent):void{
}
updateInitializedHandler() function, call the checkNow() method of the ApplicationUpdaterUI instance to start the update process and check for updates now_appUpdater.checkNow();
Normally, you would not need to execute any code once the updater is initialized. If the delay time has elapsed, the application will automatically check to see if it needs to be updated. In this case, however, the time will not have elapsed and we don't want to wait a day, so we are going to add code to check for updates now.
Next you need to package and install this new version of the application which now has update capabilities.
Next, create a new version of the application to post to your web server. You are going to change the background color of the application in the new version so after the update process, you will be able to tell instantly that the application was updated.
<versionNumber>2.0.0</versionNumber>
backgroundColor and set it equal to light gray (#e7e7e7).updateInitialized() function so the update check is not done every time the application is launched but instead by the delay you set in the update_config.xml file.protected function updateInitializedHandler(event:UpdateEvent):void{
//_appUpdater.checkNow();
}
Lastly, run the application and update it to the new version.
In Part 2 of this tutorial series, you packaged and deployed your Flex and Facebook application as a desktop application. Specifically, you created and digitally signed an AIR file, packaged the application as a native installer, created and deployed a badge installer on a web server, and installed the application using all three. Finally, you added update capability to the application, created and deployed an updated version of the application, and tested the update mechanism.
To learn more about creating and customizing Flex applications, see the Flex documentation and the Adobe Flex Developer Center. To learn more about creating and customizing AIR applications, see the AIR documentation and the Adobe AIR Developer Center.
To deploy the application as an external web application, continue with Part 3 of the tutorial series, Modify and deploy as a web application.
To deploy the application as a web application on Facebook.com, continue with Part 4 of the tutorial series, Modify and deploy on Facebook.com.