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
Do you remember the three ways that native code can send data back to ActionScript? Here they are for a quick revision:
- Returning a FREResult object from a call to FREFunction.
- Using output parameters in a FREFunction.
- Sending an event to ActionScript.
Have a look at Communication between ActionScript and native code in an ANE for details on these. If you think you’ve revised enough, let’s plunge into code. On today’s menu: sending events from Objective-C to ActionScript.
Events, sent to ActionScript, are asynchronous
Not that you should expect anything else from an event, of course. But it bears mentioning for a couple of reasons:
- there isn’t a synchronous way for native code to call an ActionScript function via the AIR C API the way that ActionScript makes direct calls to native code. When information needs to be sent to ActionScript outside a call that ActionScript initiated, your option is to send an event that ActionScript will receive asynchronously.
- you can send an event to ActionScript form any thread in your native code and it’s delivered on AIR’s single main thread. This is the only AIR C API call you are allowed to make outside the main thread.
This is the C function you call, when you need to send an event to AIR:
1 2 3 |
FREResult FREDispatchStatusEventAsync( FREContext ctx, const uint8_t* code, const uint8_t* level ); |
- ctx is a FREContext instance, which in the AIR C API is defined as a void pointer in FlashRuntimeExtensions.h:
1 |
typedef void * FREContext; |
This is a pointer to the instance of ActionScript’s flash.externalExtensionContext, which you create before you can make calls to your native code.
- code is a string, which you set. Use it to identify the event you are sending, for example “CAMERA_EVENT”.
- level is another string that you set. Use this to send more detailed information about the event to ActionScript, for example “Camera started” or “Unsupported resolution”.
How this is received by ActionScript
On the ActionScript side the target of the event is the instance of flash.externalExtensionContext, which was passed to FREDispatchStatusEventAsync. Wondering where it comes from? Read the next section.
The event’s type is flash.events.StatusEvent. You’ll notice that, among other things, StatusEvent has the following two properties:
- code : String
- level : String
See how this maps to the information sent from the C side?
You’ll need a pointer to the extension context
We saw that FREDispatchStatusEventAsync takes a pointer to an extension context. Where does that pointer come from and where does it live?
An instance of flash.externalExtensionContext is created by your ActionScript code and is passed to the Extension Context Initializer on the native side. We’ll see how this is done in detail when we look at the order of initialization in an ANE.
An example
A call to FREDispatchStatusEventAsynch from your C code:
1 2 3 4 |
FREDispatchStatusEventAsync( ctx, ( const uint8_t * ) "This is the message CODE", ( const uint8_t * ) "This is the message LEVEL" ); |
will be received in your ActionScript code in the form of a StatusEvent in a StatusEvent.STATUS handler that you provide:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
private var m_extensionContext : flash.external.ExtensionContext; ... m_extensionContext.addEventListener( StatusEvent.STATUS, onStatusEvent ); ... private function onStatusEvent( _event : StatusEvent ) : void { trace( _event.code ); trace( _event.level ); } |
This will print out:
1 2 |
This is the message CODE This is the message LEVEL |
What else?
Here’s a definition of Extension Context, if you need a reminder.
Tomorrow I’ll show you a neat trick for calling FREDispatchStatusEventAsync anywhere in your code without making it all dependent on the AIR C API.
Next we’ll see what are the Extension Context Initializer and Finalizer.
Then we will see the order of calls for all these initializers and finalizers.
Also, see how to send events from Java to ActionScript.
Over to you
Which C flavour (excuse the blasphemy) do you use or do you need to use for your native libraries? C, C++ or Objective-C? Why? Share in the comments below.
Leave a Reply