Adobe
Products

Top destinations

  • Adobe Creative Cloud
  • Creative Suite
  • Adobe Marketing Cloud
  • Acrobat
  • Photoshop
  • SiteCatalyst
  • Students
  • Elements family

Adobe Creative Cloud

  • What is Adobe Creative Cloud?
  • Design
  • Web
  • Photography
  • Video
  • Students
  • Teams
  • Enterprise
  • Educational institutions

Design and photography

  • Photoshop
  • Illustrator
  • InDesign
  • Adobe Muse
  • Lightroom

Video

  • Adobe Premiere
  • After Effects

Web development and HTML5

  • Edge Tools & Services [opens in a new window]
  • Dreamweaver
  • Gaming [opens in a new window]

Adobe Marketing Cloud

  • What is Adobe Marketing Cloud?
  • Digital analytics
  • Social marketing
  • Web experience management
  • Testing and targeting
  • Media optimization

Analytics

  • SiteCatalyst
  • Adobe Discover
  • Insight

Social

  • Adobe Social

Experience Manager

  • CQ
  • Scene7

Target

  • Test&Target
  • Recommendations
  • Search&Promote

Media Optimizer

  • AdLens
  • AudienceManager
  • AudienceResearch

Document services

  • Acrobat
  • EchoSign [opens in a new window]
  • FormsCentral [opens in a new window]
  • SendNow [opens in a new window]
  • Acrobat.com [opens in a new window]

Publishing

  • Digital Publishing Suite

  • See all products
Business solutions

By business need

  • Digital analytics
  • Digital publishing
  • Document management
  • Media optimization
  • Social marketing
  • Testing and targeting
  • Video editing and serving
  • Web development [opens in a new window]
  • Web experience management
  • See all business needs

By industry

  • Broadcast
  • Education
  • Financial services
  • Government
  • Publishing
  • Retail
  • See all industries
Support & Learning

I need help

  • Products
  • Adobe Creative Cloud
  • Adobe Marketing Cloud
  • Forums [opens in a new window]

I want to learn

  • Training and tutorials
  • Certification [opens in a new window]
  • Adobe Developer Connection
  • Adobe Design Center
  • Adobe TV [opens in a new window]
  • Adobe Marketing Center
  • Adobe Labs [opens in a new window]
Download
  • Product trials
  • Adobe Flash Player
  • Adobe Reader
  • Adobe AIR
  • See all downloads
Company
  • Careers at Adobe
  • Investor Relations
  • Newsroom
  • Privacy
  • Corporate Social Responsibility
  • Customer Showcase
  • Contact us
  • More company info
Buy
  • For personal and professional use
  • For students, educators, and staff
  • For small and medium businesses
  • Volume Licensing
  • Special offers
  • Adobe Marketing Cloud sales [opens in a new window]
Search
 
Info Sign in
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Welcome,
My Adobe
My orders
My information
My preferences
My products and services
Sign out
My cart
Privacy My Adobe
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out Privacy My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Promotions
Estimated shipping
Tax
Calculated at checkout
Total
Review and Checkout
Adobe Developer Connection / Adobe AIR Developer Center /

Transferring data with AIR Native Extensions for iOS – Part 1: Exchanging basic types between ActionScript 3 and C, C++, or Objective-C

by Tom Krcha

Tom Krcha
  • Adobe
  • gamingnotes.com

Content

  • Exchanging basic data types between ActionScript 3 and C
  • Building the native extension
  • Where to go from here

Created

25 June 2012

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
ActionScriptAdobe AIRFlash Buildergame developmentgamingiOSmobilenative extensions
Was this helpful?
Yes   No

By clicking Submit, you accept the Adobe Terms of Use.

 
Thanks for your feedback.

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.


Additional required other products

  • Xcode

User level

Intermediate

Required products

  • Flash Builder (Download trial)

Sample files

  • transferring-data-ane-ios-pt1.zip

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.


This work is licensed under a Creative Commons Attribution-Noncommercial 3.0 Unported License.

More Like This

  • Developing cross-platform Adobe AIR applications
  • Performance-tuning Adobe AIR applications
  • Using Badger for Adobe AIR applications
  • Building a native extension for iOS and Android – Part 2: Developing the ActionScript library
  • Creating your first Adobe AIR application on Android
  • Using web fonts with Adobe AIR 2.5
  • Signing Adobe AIR applications
  • Using push notifications in AIR iOS apps
  • Building Lupo: A case study in building commercial AIR applications
  • Using the Push Notifications native extension for iOS

Products

  • Adobe Creative Cloud
  • Creative Suite
  • Adobe Marketing Cloud
  • Acrobat
  • Photoshop
  • Digital Publishing Suite
  • Elements family
  • SiteCatalyst
  • For education

Download

  • Product trials
  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR

Support & Learning

  • Product help
  • Forums

Buy

  • For personal and professional use
  • For students, educators, and staff
  • For small and medium businesses
  • Volume Licensing
  • Special offers

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2013 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy | Cookies

Ad Choices

Reviewed by TRUSTe: site privacy statement