3 November 2011
Familiarity with building Adobe AIR applications for Mac OS X, including familiarity with C and Xcode.
Required third-party products
Xcode
Intermediate
This example provides a simple native extension for Mac OS X systems. The extension provides the following functionality by using ActionScript code only or by calling native code from ActionScript code:
The attached MacOS-NativeExtension-Test.zip file contains a file called instructions.txt that contains instructions for:
Directory name |
Contents |
|---|---|
00 - create cert |
Shell script to create a certificate for signing the extension |
01 - create swc |
The source and shell script to build the ActionScript side of the extension |
02 - create platform extension |
Source, Xcode project, and shell script to build the native side of the extension |
03 – create ane |
Shell script to create the ANE file. Also, contains the extension.xml file. |
04 – create dmg |
Source and shell script to build the AIR application that uses the extension |
05 – run on adl |
Shell script to run the application using adl (AIR Debug Launcher) |
The source code for the ActionScript library is in the following directory in the ZIP file:
NativeExtensions/01 - create swc/src/com/airrt/extensions
The ActionScript library contains two classes:
class Basicclass AdvancedThe Basic class provides the public method helloWorld(). The parameter mode indicates whether to:
public function helloWorld(mode:uint = 0) : String
{
if ( mode == 0 ) {
return "[Basic.as]: Hello World!";
} else {
var ctx:ExtensionContext =
ExtensionContext.createExtensionContext(
"com.airrt.extensions", "Basic" );
return String( ctx.call( "helloWorld" ) );
}
}
The Advanced class provides the remaining functionality of the extension. Each of the following public methods takes a parameter mode that indicates whether to use only the ActionScript code, or to call the native code.
reverseString()computeNthPrime()timeLongOp() which inverts an array of numbers from 1 to n. The Advanced class constructor takes one parameter, which is the size of the array. This method returns a string showing the elapsed time for the operation.The Advanced class also provides these public methods:
getElapsedTime() which computes the elapsed time for computing the nth prime number.getUnsorted() which returns a string of the first few elements of the original array that timeLongOp() inverts.getSorted() which returns a string of the first few elements of the inverted array.This extension illustrates using two extension context types:
"Basic"var ctx:ExtensionContext = ExtensionContext.createExtensionContext(
"com.airrt.extensions", "Basic" );
"Advanced"var ctx:ExtensionContext = ExtensionContext.createExtensionContext(
"com.airrt.extensions", "Advanced" );
Each context type provides a different set of native functions. Multiple extension context types are useful when an extension provides more than one set of capabilities.
The Mac OS X native extension framework is implemented in C, using the native extension C API. The Xcode project and source file for creating the native side are in the following directory in the ZIP file:
NativeExtensions/02 - create platform extension/mac/TestNativeExtension
Some implementation details to note are:
NativeExtensionsInitializer() and NativeExtensionsFinalizer() use the FREInitializer() and FREFinalizer() signatures._ctxInitializer() and _ctxFinalizer() use the FREContextInitializer() and FREContextFinalizer() signatures._ctxInitializer() uses the ctxType parameter to determine which set of native functions to make available to the ActionScript side. It chooses between the "Basic" functions and the "Advanced" functions.FREFunction() signature . These functions are _Basic_helloWorld() , _Advanced_reverseString() , _Advanced_computeNthPrime() , and _Advanced_timeLongOp() ._Advanced_timeLongOp() illustrates manipulating an ActionScript Array instance from the native side. The native function receives the array as a parameter.A sample AIR application that uses the extension is in the following directory in the ZIP file:
NativeExtensions/04 - create dmg/src
To use the extension, an AIR application does the following:
For example, the following code calls the extension's helloWorld() method when the application user selects the appropriate button in the AIR application's display:
private function onHelloWorld() : void
{
var b:Basic = new Basic();
helloWorldResult.text = b.helloWorld(useASExtCode.selected ? 0 : 1);
}
The following code calls the extension's reverseString() method when the application user selects the appropriate button:
private function onReverseString() : void
{
var a:Advanced = new Advanced();
reverseStringResult.text = a.reverseString(reverseStringSrc.text,
useASExtCode.selected ? 0 : 1);
}
For more information about developing native extensions for Adobe AIR, see:
For more information about using a native extension in an AIR application, see:
Tutorials and samples |