Requirements
Prerequisite knowledge
To make the most of this article, you’ll need a good understanding of ActionScript, C, Flash Builder, Xcode, and using tools from the command line.
Required products
Flash Builder (Download trial)
Additional required other products
User level
Intermediate

When you begin programming AIR native extensions for iOS, one of your first questions will likely be, "How do I transfer data from ActionScript 3 to C (or C++, or Objective-C) and back to ActionScript 3?" Once you understand how to do this you enter a world of endless possibilities in Adobe AIR with native extensions.
 
As you probably know already, native extensions for Adobe AIR are a combination of ActionScript classes and native code that offer access to device-specific libraries and features otherwise unavailable in the built-in ActionScript classes. For more on the basics of native extensions see Native extensions for Adobe AIR in the Adobe AIR Developer Center.
 
In this article I'll show you how to transfer basic types such as Number, int, uint, String, and Boolean data between ActionScript 3 and C. You can use the same techniques with C++, or Objective-C.
 
To follow along, you'll need Adobe Flash Builder 4.6 with AIR native extension support for ActionScript 3. You'll also need Apple Xcode to code the C part and package the static library (that is, the *.a file).
 
Figure 1 illustrates the workflow, which involves three projects. The first is a Flex Library project in Flash Builder (this is the ActionScript 3 interface for the native language code as a SWC file). The second is the native library project in Xcode (this is the C interface and implementation as a static library *.a file). The third is a test project in Flash Builder to run the native extension (*.ane).
 
Figure 1. An overview of the workflow for creating AIR native extensions
Figure 1. An overview of the workflow for creating AIR native extensions
 

 
Exchanging basic data types between ActionScript 3 and C

The code examples that follow illustrate how to transfer some basic data types from ActionScript 3 to C, process them, and return them to ActionScript 3.
 
 
Exchanging Number values
The first example uses a native language sum() function to add two Number variables.
 
Here is the ActionScript 3 code:
 
public function sum(number1:Number,number2:Number):Number{ // call C function sum with 2 arguments, number 1 and number2 and return a value var ret:Number = context.call("sum",number1,number2) as Number; return ret; }
When your native C implementation accesses an ActionScript class object or primitive data type variable it uses an FREObject variable. The runtime associates this variable with the corresponding ActionScript object.
 
Here is the corresponding C code:
 
FREObject sum(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]){ // argc ... argument count, uint // argv ... argument values, Array // retrieve the first argument and write it to a variable double number1; FREGetObjectAsDouble(argv[0], &number1); // retrieve the second argument and write it to a variable double number2; FREGetObjectAsDouble(argv[1], &number2); // add first and second number together double sum = number1 + number2; // write computed sum to a FREObject variable, which will be returned back to AS3 FREObject sumToReturn = nil; FRENewObjectFromDouble(sum, &sumToReturn); return sumToReturn; }
 
Exchanging int values
This second example finds the difference of two integer variables.
 
Here is the ActionScript 3 part:
 
public function subtract(int1:int,int2:int):int{ var ret:Number = context.call("subtract",int1,int2) as int; return ret; }
And here is the C code:
 
FREObject subtract(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]){ int32_t int1; FREGetObjectAsInt32(argv[0], &int1); int32_t int2; FREGetObjectAsInt32(argv[1], &int2); int32_t sum = int1 - int2; NSLog(@"%d-%d=%d",int1,int2,sum); FREObject sumToReturn = nil; FRENewObjectFromInt32(sum, &sumToReturn); return sumToReturn; }
 
Exchanging uint values
This example uses uint values to calculate a product.
 
Here is the ActionScript 3 code:
 
public function multiply(uint1:uint,uint2:uint):uint{ var ret:Number = context.call("multiply",uint1,uint2) as uint; return ret; }
Here is the C code:
 
FREObject multiply(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]){ uint32_t uint1; FREGetObjectAsUint32(argv[0], &uint1); uint32_t uint2; FREGetObjectAsUint32(argv[1], &uint2); uint32_t sum = uint1*uint2; NSLog(@"%d*%d=%d",uint1,uint2,sum); FREObject sumToReturn = nil; FRENewObjectFromUint32(sum, &sumToReturn); return sumToReturn; }
 
Exchaging String values
In this example I go a step further and use an Objective-C function for concatenating strings. So, this one also demonstrates how to pass strings between C and Objective-C.
 
Here is the ActionScript 3 code:
 
public function concatenate(str1:String,str2:String):String{ var ret:String = context.call("concatenate",str1,str2) as String; return ret; }
Here is the C and Objective-C code:
 
FREObject concatenate(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]){ // To be filled uint32_t string1Length; const uint8_t *string1; FREGetObjectAsUTF8(argv[0], &string1Length, &string1); uint32_t string2Length; const uint8_t *string2; FREGetObjectAsUTF8(argv[1], &string2Length, &string2); // Convert C strings to Obj-C strings NSString *string1ObjC = [NSString stringWithUTF8String:(char*)string1]; NSString *string2ObjC = [NSString stringWithUTF8String:(char*)string2]; // Concat strings NSString *returnString = [string1ObjC stringByAppendingString:string2ObjC]; // Convert Obj-C string to C UTF8String const char *str = [returnString UTF8String]; // Prepare for AS3 FREObject retStr; FRENewObjectFromUTF8(strlen(str)+1, (const uint8_t*)str, &retStr); // Return data back to ActionScript return retStr; }
 
Exchanging Boolean values
The final example simply retruns the inverse of the Boolean value passed to it.
 
Here is the ActionScript 3 function:
 
public function opposite(bool:Boolean):Boolean{ var ret:Boolean = context.call("opposite",bool) as Boolean; return ret; }
And here is its C counterpart:
 
FREObject opposite(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]){ uint32_t boolean; FREGetObjectAsBool(argv[0], &boolean); uint32_t oppositeValue = !boolean; FREObject retBool = nil; FRENewObjectFromBool(oppositeValue, &retBool); return retBool; }

 
Building the native extension

To compile the native extension (that is, the .ane file) I used the following commands at the command line:
 
unzip -o IOSExtension.swc /PATH/TO/FLEX_AIR_SDK/bin/adt -package -target ane IOSExtension.ane extension.xml -swc IOSExtension.swc -platform iPhone-ARM library.swf libIOSExtension.a
Here are the contents of my extension.xml file:
 
<extension xmlns="http://ns.adobe.com/air/extension/2.5"> <id>com.krcha.IOSExtension</id> <versionNumber>1</versionNumber> <platforms> <platform name="iPhone-ARM"> <applicationDeployment> <nativeLibrary>libIOSExtension.a</nativeLibrary> <initializer>ADBEExtInitializer</initializer> <finalizer>ADBEExtFinalizer</finalizer> </applicationDeployment> </platform> </platforms> </extension>

 
Where to go from here

If you want to learn more on the details of native extensions, a good starting point is the Native C API Reference for Adobe AIR extensions. For more on using C for your native language, see Coding the native side with C.
 
Part 2 of this series covers more complex data types, including Array and Vector objects.