Native extension of air for iOS devices

Source: Internet
Author: User

Source: http://www.cnblogs.com/alex-tech/archive/2012/03/22/2411264.html

ANE component

In the iOS platform, the components of ANE are basically divided into the As 3.0 Extension class library and the Obj-c native Extension class library two parts, which are packaged to generate an air extension file (. ane), and then packaged into an iOS native app IPA file as part of the. swf.

Action Script Class Library build

The as extension part of the ANE is a Swc,air 3.0 SDK that adds a new method to the Flash.external.ExtensionContext class, where the class name is Nativealert.

1) Development environment, Adobe AIR 3.0\3.1 SDK, Flash Builder or Flash Develop.

2) Build the Flex Library project, compile with the air library, the link is set as the default external link.

3) Main class Nativealert and nativealertevent.

4) finally get NATIVEALERT.SWC

Main work in Nativealert:

    • Defines the extension unique ID;
    • Initializes the context environment by specifying the ID, and obtains the instance;
    • The method decode defined in the native class is called according to the method name encodebmp derived from the ane, with the parameters ByteArray and int,int;
   1: Package  com.wanghui.nativeextensions
   2:  {
   3:      import Flash.external.ExtensionContext;
   4:      import Flash.utils.ByteArray;
   5:      
   6:      class Imageprocessor
   7:      {
   8:          var context:extensioncontext;
   9:          
  Ten:          function imageprocessor ()
  One:          {
  :              context = Extensioncontext.createextensioncontext (//@Attention [1]
  :          }
  :          function decode (data:bytearray)
  :          {
  :               var bytearray:bytearray = data;
  N:               var transparent:int     = Bytearray.readunsignedbyte (); 
  :               //Call the native class's Decode method
  :               var handler:int         int (context.call (//@Attention [2]   
  :          }
  :      }
  :  }

Extensioncontext uses the static method Createextensioncontext () to obtain an instance, The parameter com.wanghui.nativeextensions.myextension is the ID of this extension, which is very important, and it needs to be paired with this ID in the extended configuration file and in the application description file.

Calls to methods defined in the native class can be implemented using the method call (), which can have a return value because it is a synchronous invocation. You can also add event listeners to the Extensioncontext class to obtain the events that are distributed back from the native class.

Obj-c Local Extension

1) Development environment Xcode

2) establish Cocoa Touch Static Library;

3) Select Build Settings, check preprocessor Macros, Remove all tags, such as debug=1 and ${inherited};

4) Select build Settings, set Enable linking with shared libraries to YES;

5) introduce the header file FlashRuntimeExtensions.hin the Air SDK;

6) Native class export method decode

7) finally get DECODER.A

The Decode method is defined to return the Freobject method, Freobject is the interface type and the parameter type is as follows. It is important to note that the interface with as includes the function return value, which is defined as the Freobject type , such as the Handlerobject in the code.

   1:  //CTX for the context ID,ARGC is the number of parameters, argv is the parameter information
   2:  void* funcdata, uint32_t argc, Freobject argv[])
   3:  {
   4:      Frebytearray ByteArray;
   5:      int position,transparent;
   6:      //Get parameter information
   7:      Freacquirebytearray (Argv[0],&bytearray);
   8:      FREGetObjectAsUint32 (Argv[1], (uint32_t*) &position);
   9:      FREGetObjectAsUint32 (Argv[2], (uint32_t*) &transparent);
  Ten:      Handler *handler=malloc (sizeof (Handler));
  11:      
  Acquir:      //After the release
  :      Frereleasebytearray (argv[0]);
  :      freobject handlerobject;
  :      //apply for return data space
  :      FRENewObjectFromUint32 ((uint32_t) handler,&handlerobject);
  :      return handlerobject;      
  :  }

To define the Decode method as an interface method, you export it in Contextinitializer, specifying the number of methods to export, method name:

   1:  void Contextinitializer (const uint8_t* Ctxtype, Frecontext ctx, 
   2:          const frenamedfunction** functionstoset) {
   3:      inittables ();
   4:      //number of interfaces defined
   5:      *numfunctionstotest = 1;
   6:      //Defines an instance of the Frenamedfunction type Func, the number of initialization functions
   7:      frenamedfunction* func = (frenamedfunction*) malloc (sizeof (frenamedfunction) * 1);
   8:      //Defines an interface, name is the string "decode", and the function body is decode
   9:      func[0].name = ("decode";
  Ten:      func[0].functiondata = NULL;
  One:      func[0].function = &decode;
  :      *functionstoset = func;
  :  }

The Contextinitializer method is specified in the initialization function Extinitializer of the native extension class:

   1:  void Extinitializer (void** extdatatoset,frecontextinitializer* Ctxinitializertoset, frecontextfinalizer* ctxfinalizertoset) { 
   2:      *extdatatoset = NULL;
   3:      *ctxinitializertoset = &ContextInitializer;
   4:      *ctxfinalizertoset = &ContextFinalizer;
   5:  }

Extinitializer is a native extension of the program portal, which can be defined by extending the configuration file Extension.xml:

   1:  <extension xmlns="http://ns.adobe.com/air/extension/2.5" >
   2:      <id>com.wanghui.nativeextensions.myextension</id>          <[email protected] Attention [3] Extension ID >
   3:      <versionNumber>0.0.1</versionNumber>
   4:      <platforms>
   5:          <platform name="Iphone-arm" >
   6:              <applicationDeployment>
   7:                  <nativeLibrary>decoder.a</nativeLibrary> <[email protected] Attention [4] Local extension Library name >
   8:                  
   9:                  <finalizer>ExtFinalizer</finalizer>
  Ten:              </applicationDeployment>
  One:          </platform>
Pack ane

1) decoder.a

2) NATIVEALERT.SWC

3) NATIVEALERT.SWC Extract the library.swf

4) Extended Description file Extension.xml

5) Use ADT package provided by air to get Decoder.ane

Adt-package  -target ane decoder.ane extension.xml-swc nativealert.swc-platform iphone-arm library.swf decoder.a
Packaging IPA

1) Write test program (Flex Mobile Project), use class library NATIVEALERT.SWC, choose the way of external link SWC, get testmobile.swf; application description file Testmobile-app.xml, add the following description:

   1:  <extensions>
   2:  <[email protected] Attention [5] Extended id>
   3:  
   4:  </extensions>

1) decoder.ane, stored in ext directory

2) Developer Device authorization file Ceshi.mobileprovision

3) Developer Signing certificate file DEVELOPERKEY.P12

4) Packaging IPA to get Example.ipa

/adt-package-target ipa-test-interpreter-provisioning-profile Ceshi.mobileprovision-storetype Pkcs12-keystore Developerkey.p12-storepass 1234 Example.ipa Testmobile-app.xml testmobile.swf-extdir ext
Note:

1) @ ATTENTION[1] [3] [5] location extension ID must be consistent;

2) The simulator cannot be run, debugging can choose the way to output debugging information;

3) If white screen exits immediately after running the program, OK [obj-c Local extension Section] 3) 4) two settings are correct;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.