Buy any Easy Native Extensions 2nd Edition package and get our $99 iOS + Android ANE Template completely free before the end of June 2015.
- step-by-step guide to making your iOS extension in under an hour
- library for data conversion between ActionScript and native code
- tutorials
- infographics
- code included
So you already know that, in order to make calls from your AIR app into native code, you need an Extension Context. One of the Extension Context roles is to tell AIR what native functionality is available for calling from ActionScript and this happens during the Extension Context initialization. This post will show you how this is done in C – you can use that in your C, C++ or Objective-C code.
FREContextInitializer
FREContextInitializer is an AIR C API function, which is called to initialize your Extension Context when it’s created. You provide an implementation for this function in your native code, following this signature:
1 2 3 4 5 6 7 |
typedef void (*FREContextInitializer)( void* extData , const uint8_t* ctxType , FREContext ctx , uint32_t* numFunctionsToSet, const FRENamedFunction** functionsToSet ); |
extData – this parameter might look familiar. This is the same data that you set in your Extension Initializer function.
ctxType – this is a string parameter, which lets you specify a context type. You set this as a String parameter in your ActionScript createExtensionContext call. In your FreContextInitializer implementation you have the option to set the context up differently, depending on the context type: you can expose one set of functions for one type and another set of functions – for another type.
ctx – the Extension Context you are initializing.
numFunctionsToSet – this tells AIR how many functions you will make available through the extension context.
functionsToSet – this is an array of FRENamedFunction objects, each of which tells AIR about a native function that you make available for calling from ActionScript.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
void ScreenCastContextInitializer( void * extData, const uint8_t* ctxType, FREContext ctx, uint32_t* numFunctionsToSet, const FRENamedFunction** functionsToSet ) { static FRENamedFunction extensionFunctions[] = { { (const uint8_t*) "as_startRecording", NULL, &ASStartRecording }, { (const uint8_t*) "as_finishRecording", NULL, &ASFinishRecording }, { (const uint8_t*) "as_recordFrame", NULL, &ASRecordFrame } }; *numFunctionsToSet = sizeof( extensionFunctions ) / sizeof( FRENamedFunction ); *functionsToSet = extensionFunctions; } |
In this example we ignore the context type and expose three native functions to be called from ActionScript.
“as_startRecording” is how ActionScript will refer to the function when it needs to call it. &ASStartRecording is a pointer to the native implementation of the same function.
FREContextFinalizer
I bet you’ll be surprised to hear that FREContextFinalizer is called when your Extension Context is being disposed of. Were you?
The context finalizer is a function that you implement, following this signature:
1 2 3 |
typedef void (*FREContextFinalizer)( FREContext ctx ); |
where ctx is the context that’s being destroyed.
Example:
1 2 3 4 |
void ScreenCastContextFinalizer( FREContext ctx ) { return; } |
What else?
Next, we’ll have a look at the Java equivalent of the context initialization and finalization.
Then we’ll attempt to put some order in this bunch of initializers and see a graphic of how this happens and who calls who.
After that it will be the finalization’s turn: we’ll look at the order of calls when an extension is unloaded.
Over to you
At what stage is your ANE at the moment? What’s stopping you from finishing it? Let us know in the comments below.
bkuperberg
Hi,
great site and nice tutorials !
I’ve been creating ANE for Windows for almost 2 years, and i’m currently trying to make my latest ANE working on MacOS too.
I’ve been compiling the .framework in XCode 5 (on Mavericks), created the .ane, successfully compiled a test project, but the ane functions are not recognized (the famous error #3500 “The extension context does not have a method with the name […])
I’ve searched everywhere, followed a lot of explanations but those articles are often too old (XCode 4, base SDK target OSX10.5..) and mainly explaining how to create iOS extensions.
You seem to be up to date and not having any problems creating extensions for OSX today, what is your secret ? Would it be possible to create a small tutorial on how to configure XCode 5 for an OSX native Extension, and maybe an XCode template ?
Thank you very much, i have at least 5 extensions i would like to put on your site, but i would like to make them cross platform before.
Radoslava
Hi bkuperberg,
I wonder whether you get the infamous error message when you debug, but not in a release build. If that’s the case, you’ve run into an AIR bug, which causes a confusion with where the files in the ANE package are on Mac OS.
Thanks for the idea to do a tutorial on that – challenge accepted.
In the mean time, see if this might provide some help. There is a great post on Adobe’s forums by sbaldizz: http://forums.adobe.com/message/4391394#4391394
Let us know how it goes.
Radoslava
benkuper
Thank you Radoslava,
actually i managed to get an ANE compiling and working by starting off a new project from a working osx extension. I think this is due to some obscure settings in XCode making the exported functions inaccessible or invisible from AIR…
I didn’t digg further because i have to work on the osx ANE now, but if i find the faulty setting i will post it there.
Thank you very much
Ben