[IOS] [Air] Air native extensions for iOS devices

Source: Internet
Author: User
Document directory
  • Ane Components
  • Construct the Action Script class library
  • OBJ-clocal Extension
  • Package ANE
  • Package IPA
Ane Components

On the iOS platform, ANE consists of two types: As 3.0extended category library and obj-c0000extended category library. These two types are packaged to generate an airextension file (.ane.pdf, which is then packaged into an IPA file of the IOS native application with .swf.

 

Construct the Action Script class library

The as extension of ANE is a SWC, and the air 3.0 SDK adds a new method for the flash. External. extensioncontext class. The class name here is nativealert.

1) Development Environment: Adobe AIR 3.0 \ 3.1 SDK, flash builder or flash develop.

2) create a flex Library Project, including the air library during compilation, and set the link mode to the default external link.

3) nativealert and nativealertevent.

4) Finally, the nativealert. SWC is obtained.

Nativealert mainly works in:

  • Define the unique ID of the extension;
  • Initialize the Context Environment by specifying the ID to obtain the instance;
  • Call the decode defined in the native Class Based on the exported method name encodebmp in ane. The parameters are bytearray, Int, and Int;
   1:  package com.wanghui.nativeextensions
   2:  {
   3:      import flash.external.ExtensionContext;
   4:      import flash.utils.ByteArray;
   5:      
   6:      public class ImageProcessor
   7:      {
   8:          private var context:ExtensionContext;
   9:          
  10:          public function ImageProcessor()
  11:          {
  12:              context = ExtensionContext.createExtensionContext('com.wanghui.nativeextensions.myextension', ''); //@Attention[1]
  13:          }
  14:          public function decode(data:ByteArray)
  15:          {
  16:               var byteArray:ByteArray = data;
  17:               var transparent:int     = byteArray.readUnsignedByte();
18: // call the decode method of the native class
  19:               var handler:int         = int(context.call("decode",byteArray,byteArray.position,transparent)); //@Attention[2]
  20:          }
  21:      }
  22:  }

Extensioncontext obtains an instance through the static createextensioncontext () method,The parameter com. wanghui. nativeextensions. myextension is the ID of this extension. It is very important to use this ID in the extension configuration file and the application description file.

The call () method can be used to call the methods defined in the native class. Because the call is synchronous, the function can return values. You can also add event listening to The extensioncontext class to obtain the events distributed from the native class.

OBJ-clocal Extension

1) Development Environment xcode

2)CreateCocoa touch static library;

3)SelectBuild settings, SelectPreprocessor macros,Remove all tags, suchDEBUG = 1 and $ {inherited};

4)SelectBuild settings, SetEnable linking with shared librariesIsYes;

5)Introduce the header file IN THE AIR SDKFlashruntimeextensions. h;

6) Export method decode in native class

7) the final result is decoder..

The decode method is defined as the return freobject method. freobject is an interface type and the parameter type is as follows.Note that the interfaces with the as interface, including the function return value, must be defined as the freobject type.For example, handlerobject in the code.

1: // CTX indicates the context ID, argc indicates the number of parameters, and argv indicates the parameter information.
   2:  FREObject decode(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[])
   3:  {
   4:      FREByteArray byteArray;
   5:      int position,transparent;
6: // obtain the parameter information
   7:      FREAcquireByteArray(argv[0],&byteArray);
   8:      FREGetObjectAsUint32(argv[1],(uint32_t*)&position);
   9:      FREGetObjectAsUint32(argv[2],(uint32_t*)&transparent);
  10:      Handler *handler=malloc(sizeof(Handler));
  11:      
12: // perform release after acquir
  13:      FREReleaseByteArray(argv[0]);
  14:      FREObject handlerObject;
15: // apply to return data space
  16:      FRENewObjectFromUint32((uint32_t)handler,&handlerObject);
  17:      return handlerObject;      
  18:  }

To define the decode method as an interface method, you must export it in contextinitializer to specify the number of exported methods. Method Name:

   1:  void ContextInitializer(void* extData, const uint8_t* ctxType, FREContext ctx,
   2:          uint32_t* numFunctionsToTest, const FRENamedFunction** functionsToSet){
   3:      initTables();
4: // number of defined interfaces
   5:      *numFunctionsToTest = 1;
6: // define a frenamedfunction-type instance func, the number of initialized Functions
   7:      FRENamedFunction* func = (FRENamedFunction*) malloc(sizeof(FRENamedFunction) * 1);
8: // define an interface. The name is the string "decode"; and the function body is decode.
   9:      func[0].name = (const uint8_t*) "decode";
  10:      func[0].functionData = NULL;
  11:      func[0].function = &decode;
  12:      *functionsToSet = func;
  13:  }

The contextinitializer method is specified in the native extension class's initialization function extinitializer:

   1:  void ExtInitializer(void** extDataToSet,FREContextInitializer* ctxInitializerToSet,FREContextFinalizer* ctxFinalizerToSet){
   2:      *extDataToSet = NULL;
   3:      *ctxInitializerToSet = &ContextInitializer;
   4:      *ctxFinalizerToSet = &ContextFinalizer;
   5:  }

Extinitializer is the entry to the native extension program. It can be defined through the extension configuration file extension. xml:

   1:  <extension xmlns="http://ns.adobe.com/air/extension/2.5">
2: <ID> com. wanghui. nativeextensions. myextension </ID> <! -- @ Attention [3] extension ID>
   3:      <versionNumber>0.0.1</versionNumber>
   4:      <platforms>
   5:          <platform name="iPhone-ARM">
   6:              <applicationDeployment>
7: <nativelibrary> decoder. A </nativelibrary> <! -- @ Attention [4] Name of the local extension library>
   8:                  <initializer>ExtInitializer</initializer> 
   9:                  <finalizer>ExtFinalizer</finalizer>
  10:              </applicationDeployment>
  11:          </platform>
Package ANE

1) decoder.

2) nativealert. SWC

3) nativealert.swcdecompressed library.swf

4) extended description file extension. xml

5) use the 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
Package IPA

1) write the test program (flexmobile project, use the class library nativealert.swc, select the external chain to connect swc's formula, get to testmobile.swf; application description file testmobile-app.xml, add the following description:

   1:  <extensions>
2: <! -- @ Attention [5] extension ID>
   3:  <extensionID> com.wanghui.nativeextensions.myextension </extensionID> 
   4:  </extensions>

1) decoder. Ane, which is stored in the ext directory

2) Developer device authorization file Ceshi. mobileprovision

3) The developer signs the Certificate file named keystore key. p12.

4) package 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] The extension IDs must be consistent;

2) The simulator cannot run. You can select the method to output debugging information for debugging;

3) after running the program, if the white screen immediately exits, OK[Obj-clocal extension] 3) 4)The two settings are correct;

Related Article

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.