Sending events from Objective-C to ActionScript

This is the third post of our Translating between ActionScript and Objective-C series and the first one in which you will get your hands dirty with code.

What was covered so far?

What will this part cover?

In this and the next four articles you will start by building an arsenal of auxiliary functions that turn lengthy Objective-C code into one-liners that you can reuse in the native part of your iOS ANEs. Code you will write will be Objective-C and will go into the Objective-C library that’s part of your ANE.

The goal of this article is that you end up being able to send an event to ActionScript with this short line of code:

Info: For a comprehensive data conversion guide and how to make it into a library download our eBook “iOS vs. ActionScript Data Types Guide”.

Sending events to ActionScript

Sending an asynchronous event is one of the three ways of passing data from native code back to ActionScript. Have a look at this post for the other two ways.

Sending an event is the only asynchronous way of passing data back and the only C API AIR call you can make on any thread other than the main one. It is your way of signalling to your ActionScript code when something has gone wrong or when the result of an asynchronous operation is ready, for example retrieving user names from a server.

What effectively happens is this:

1. You subscribe to status events delivered to your ExtensionContext instance in ActionScript:

ActionScript
Then you call FREDispatchStatusEventAsync in your native code whenever you need to send an event that will be caught by that listener:

Objective-C
FREDispatchStatusEventAsync takes a FREContext – a pointer to the extension context on the native side and two string values, which are mapped to the StatusEvent code and level properties respectively:
So the first auxiliary function you add to your reusable library will be one that lets you send an event/message to ActionScript from anywhere without having to worry about supplying an extension context or converting strings to const uint8_t* .

For this purpose you will need your auxiliary function to have access to a context instance. I tend to make a copy of the one passed to the extension context initializer and keep it in my ANE’s main native file as a global variable. Makes sense to use that instead of having another copy lurking around. In the declaration below the extern keyword makes that global copy available to the code in the file where you are building your reusable library of functions:

Since we are in a declaring mood (or is it just me?), let’s declare a few event types too:

And finally, here is the auxiliary function you have been waiting for:

Result

Now you can do this much shorter call in any part of your code:

Additional information

What’s next?

Our next article will show you how to implement a one-liner for one-liner for checking FREResult. See you there!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">