[ios][air] AIR面向IOS設備的原生擴展


ANE組成部分


在IOS平台中,ANE的組成部分基本分為AS 3.0擴展類庫和Obj-C原生擴展類庫兩個部分,這兩個部分打包后生成AIR擴展文件(.ane),最后和.swf起打包成IOS原生應用IPA文件。

 未命名

Action Script類庫構建


ANE的AS擴展部分是一個SWC,AIR 3.0 SDK里為flash.external.ExtensionContext類添加了新的方法,這里類名為NativeAlert.

1) 開發環境, Adobe AIR 3.0\3.1 SDK, Flash Builder或Flash Develop.

2) 建立Flex 庫項目,編譯的時候包括AIR庫,鏈接方式設定為默認的外部鏈接.

3) 主要類NativeAlert與NativeAlertEvent.

4) 最終得到NativeAlert.swc

NativeAlert中主要工作:

  • 定義擴展唯一ID;
  • 通過指定ID,初始化上下文環境,獲取實例;
  • 根據ane中導出的方法名encodeBMP調用原生類中定義的方法decode,參數為ByteArray和int,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:               // 調用原生類的decode方法
  19:               var handler:int         = int(context.call("decode",byteArray,byteArray.position,transparent)); //@Attention[2]
  20:          }
  21:      }
  22:  }

ExtensionContext通過靜態方法createExtensionContext()來獲得一個實例,參數com.wanghui.nativeextensions.myextension是這個擴展的ID,它非常重要,在擴展的配置文件里和應用程序描述文件中都需要用這個ID進行配對。

調用原生類中定義的方法可以用方法call()來實現,由於是同步調用,所以函數可以有返回值。還可以給ExtensionContext類添加事件偵聽,用來獲取從原生類中派發回來的事件。

Obj-C本地擴展


1)開發環境XCode

2) 建立Cocoa Touch Static Library;

3) 選擇build settings,選中 Preprocessor Macros,移除所有標記,諸如DEBUG=1 and ${inherited};

4) 選擇build settings,設定Enable linking with shared librariesYES;

5) 引入AIR SDK中的頭文件FlashRuntimeExtensions.h;

6) 原生類中導出方法decode

7) 最終得到decoder.a

decode方法定義為返回FREObject方法,FREObject是接口類型,參數類型如下。這里要注意,與AS的接口包括函數返回值,都要定義成FREObject類型,比如代碼中的handlerObject。

   1:  // ctx為上下文id,argc為參數個數,argv中為參數信息
   2:  FREObject decode(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[])
   3:  {
   4:      FREByteArray byteArray;
   5:      int position,transparent;
   6:      // 獲取參數信息
   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:      // Acquir之后進行Release
  13:      FREReleaseByteArray(argv[0]);
  14:      FREObject handlerObject;
  15:      // 申請返回數據空間
  16:      FRENewObjectFromUint32((uint32_t)handler,&handlerObject);
  17:      return handlerObject;      
  18:  }

要將decode方法定義為接口方法,需在ContextInitializer進行導出,指定導出的方法數,方法名稱:

   1:  void ContextInitializer(void* extData, const uint8_t* ctxType, FREContext ctx,
   2:          uint32_t* numFunctionsToTest, const FRENamedFunction** functionsToSet){
   3:      initTables();
   4:      //定義的接口的數量
   5:      *numFunctionsToTest = 1;
   6:      //定義一個FRENamedFunction類型的實例func,初始化函數的個數
   7:      FRENamedFunction* func = (FRENamedFunction*) malloc(sizeof(FRENamedFunction) * 1);
   8:      //定義一個接口,name是字符串"decode";,函數體是decode
   9:      func[0].name = (const uint8_t*) "decode";
  10:      func[0].functionData = NULL;
  11:      func[0].function = &decode;
  12:      *functionsToSet = func;
  13:  }

ContextInitializer方法是在原生擴展類的初始化函數ExtInitializer中指定的:

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

ExtInitializer是原生擴展的程序入口,它可以通過擴展配置文件extension.xml來定義:

   1:  <extension xmlns="http://ns.adobe.com/air/extension/2.5">
   2:      <id>com.wanghui.nativeextensions.myextension</id>          <!--@ Attention [3]擴展ID>
   3:      <versionNumber>0.0.1</versionNumber>
   4:      <platforms>
   5:          <platform name="iPhone-ARM">
   6:              <applicationDeployment>
   7:                  <nativeLibrary>decoder.a</nativeLibrary>       <!--@ Attention [4]本地擴展庫名稱>
   8:                  <initializer>ExtInitializer</initializer> 
   9:                  <finalizer>ExtFinalizer</finalizer>
  10:              </applicationDeployment>
  11:          </platform>

打包ANE


1) decoder.a

2) NativeAlert.swc

3) NativeAlert.swc解壓得到的library.swf

4) 擴展描述文件extension.xml

5) 利用AIR提供的adt打包,得到decoder.ane

adt -package  -target ane decoder.ane extension.xml -swc NativeAlert.swc -platform iPhone-ARM library.swf decoder.a

打包IPA


1) 寫測試程序(flex手機項目),使用類庫NativeAlert.swc,選擇外部鏈接swc的方式,得到testmobile.swf;應用程序描述文件testmobile-app.xml,添加如下描述:

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

1) decoder.ane,存放在ext目錄下

2) 開發者設備授權文件 ceshi.mobileprovision

3) 開發者簽名證書文件developerkey.p12

4) 打包ipa得到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

注:


1) @ Attention[1] [3] [5]位置的擴展ID一定要一致;

2) 模擬器中無法運行,調試可以選擇輸出調試信息的方式;

3) 運行程序后如果白屏立刻退出,確定[Obj-C本地擴展部分的] 3)4)兩項設置是正確的;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM