How to send events from Objective-C to ActionScript

Opt In Image
Early bird offer on the ANE eBooks

 

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:

  1. Returning a FREResult object from a call to FREFunction.
  2. Using output parameters in a FREFunction.
  3. 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:

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:

will be received in your ActionScript code in the form of a StatusEvent in a StatusEvent.STATUS handler that you provide:

This will print out:

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.


Warning: count(): Parameter must be an array or an object that implements Countable in /home/easyna6/public_html/easynativeextensions_wp/wp-includes/class-wp-comment-query.php on line 399

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="">