Thursday, December 26, 2013

Creating Adobe - AIR Native Extension for iOS

Holla Amigos !,

In todays blog session I'm going to discuss the  procedure to generate Adobe AIR ANE (Adobe AIR native extension) for iOS devices. Before going further let me first tell you something about AIR and ANE.


What is Adobe AIR?
    The Adobe® AIR® runtime enables developers to package the same code into native apps for iPhone, iPad, Kindle Fire, Nook Tablet, and other Android™ devices, reaching the mobile app stores for over 500 million devices.

What is a native extension?

      Native Extensions for Adobe AIR are code libraries that contain native code wrapped with an ActionScript API. Native extensions provide easy access to device-specific libraries and features that are not available in the built-in ActionScript classes. Examples of native extensions include making a mobile device vibrate, integrating ad-networks and in-app purchasing systems into your games, and using the iOS GameCenter in your iOS games.



This blog also explains one of the most important feature called Method Swizzling and its usage. This was posted by me long back in a Facebook forum.  (https://www.facebook.com/groups/330366610363679/481424851924520)


Now let us start with a demo ANE creation example. We will be creating an ANE to open my blog in iPhone and to show a welcome alert when the application will resign active . We will her do method swizzling for  Application delegate method applicationWillResignActive: .

Here are the steps:

1) Open Xcode and create a new  project to make a cocoa static library. Name it  BlogViewerLib.

2) Add FlashRuntimeExtensions.h from /Applications/Adobe Flash Builder 4.6/sdks/4.6.0/include

3) Modify your BlogViewerLib-Prefix.pch as follows:












Here we imported necessary headers. objc package classes are required for method swizzling.


3) Modify your BlogViewerLib.h as follows:



4) Modify your BlogViewerLib.m as follows:











Lots of coding!!!!! lets see what we have done in BlogViewerLib class. The class inherits the NSObject class and implements UIApplicationDelegate and UIAlertViewDelegate protocol. UIApplicationDelegate is to implement the application delegate method applicationWillResignActive: .

I didn't used UIAlertViewDelegate methods but if you want you can implement its method. So its optional for this project.

I used a macro definition of a method named DEFINE_ANE_FUNCTION which returns an FREObject (please visit this link.1) and takes following parameters:


FREContext ctx   : please visit this link.2
void* funcData  :
uint32_t argc 
 FREObject argv[] 

please visit this link.3

Then we have defined a blank implementation of applicationWillResignActive: . Inside the implementation block you can define other delegate method also.  customApplicationWillResignActive is the custom method which will replace the delegate method applicationWillResignActive: . It has FREDispatchStatusEventAsync( ) , which send a callback to AS3.0

Please check the implementation of customApplicationWillResignActive() and BlogViewerContextInitializer() to see how method swizzling is performed.


Next you need to implement the contextInitiallizer , contextFinallizer, extesionInitiallizer and extensionFinallizer methods.

please visit this link.4
please visit this link.5


5) Go to edit scheme and use release. Now Clean the build and build. You will get libBlogViewerLib.a .

6) Now create a flex library project. Include AIR SDK  in the project settings. Check Flash builder project in Blogger_AS3 and a project in default folder. Both are the projects with same name . But the one in default has blank implementation.

I this project we have created an extension with id "com.dreams.dreamsbloggerane" . we will use this extension to call method implemented in our objective-C library. Be careful, with the method name. It should be same as the name declared for each FREFunction() in contextInitializer().

The class BlogViewerExtension extends the EventDispatcher, and implements the onStatus() method, which handles the events dispatched from library  by FREDispatchStatusEventAsync().




























7) Check your extension.xml file in ANE_GENERATOR. 



















It defines properties for default and iPhone-ARM platform.

8 ) Building both projects you will get Blogger_AS3.swc files . If you unzip this you will get library file. Get these two library files and keep in iOS and default folders in ANE_GENERATOR. Copy pate the  libBlogViewerLib.a file in iOS folder. SWC file builded for iPhone-ARM must be kept in ANE_GENERATOR. Now cd to ANE_GENERATOR and run following cmd.

admins-MacBook-Pro:ANE_GENERATOR prashantp$ /Applications/Adobe\ Flash\ Builder\ 4.6/sdks/4.6.0_3.9/bin/adt -package -target ane BlogViewerLib.ane extension.xml -swc Blogger_AS3.swc -platform iPhone-ARM -C ios . -platform default -C default .

please visit this link.6



9) You will get .ane file in ANE_GENERATOR . You can now use this as a native lib in your AS3.0 AIR-iOS project. Please check testBlogger project.




source download : download