Wednesday, February 15, 2012

Support for External Frameworks and Exception Handling in AIR Native Extensions for iOS

by Nikhil Bharadwaj

Recently, I worked on an exciting and a challenging project. The client requirement was to develop a flex app using native extensions for iOS. As you all know, AIR native extensions were introduced couple of months ago and, like every other developer, I started my research but couldn't find much information on how these extensions work in general. After a few weeks of working on this, I wrote the native extension layer in iOS and generated a .ANE extension file. The output of a native extension package file is .ANE. I imported the extension file in the flash builder and when I tried to build the app, the packager failed and the result was ld64 error. After some research, I found the problem is with the "CoreTelephony" framework in the native layer.

According to Adobe, the frameworks that can be linked during runtime are:

  • CoreFoundation
  • System Configuration
  • Core Location
  • Foundation
  • UIKit
  • AudioToolbox
  • CoreMedia
  • OpenGLES
  • MobileCoreServices
  • CFNetwork
  • CoreVideo
  • Security
  • CoreGraphics
  • QuartzCore
  • AVFoundation

Starting from AIR 3.1, any frameworks other than the above stated or shared libraries and linker options can be linked at runtime when generating an .ANE file.

The approach I took to solve this problem: In your extension code on flex layer create an xml file (for ex: platformOptions.xml) to link the external frameworks.

<?xmlversion="1.0"encoding="UTF-8"?>
<platform xmlns="http://ns.adobe.com/air/extension/3.1">
   <sdkVersion>5.0</sdkVersion>
   <linkerOptions>
      <!-- Specify the minimum iOS version -->
      <option>-ios_version_min 4.0 </option>
      <!-- Include the framework that you want to link with -framework option -->
      <option>-framework CoreTelephony</option>
      <!--Include any shared libraries -->
      <option>-libShared</option>
   </linkerOptions>
</platform>

For the linker to link this file at runtime, use -platformoptions option when packaging an ANE. In your terminal window, the command should be:

adt -package -target ane test.ane extension.xml -swc test.swc -platform iPhone-ARM -C release -platformoptions platformOptions.xml

Native extensions are supported starting from AIR 3.0 on iOS devices 4.0 and above.

Exception Handling

Unfortunately any exceptions thrown on native code cannot be caught by the AIR layer which results in the app crashing. As a workaround, I found a global exception handler in iOS. With the help of this code, after an exception is caught on native layer, I pass the exception as a delegate message to the flex layer. By implementing this, you at least know what the exception is and you can write some conditional logic on the flex layer to effectively handle these messages.

If you have any questions, please feel free to contact me at nbharadwaj@solstice-consulting.com. Any suggestions are appreciated.

0 comments:

Post a Comment