[AIR] NativeExtension在IOS下的开发实例 --- IOS项目的创建 (一)

来源:http://bbs.9ria.com/thread-102037-1-1.html

最近看到本版块的很多关于NativeExtension的应用。但是都是在Android下面的应用。也有很多朋友在线上问我这个问题,今天晚上特写了一篇NativeExtension在IOS的应用,首先,我们做准备工作,下载AIR3.0及其SDK,当然我们也可以下载Flash Builder 4.6的Prelease版本,4.6可以直接添加ANE文件,进行最后的打包Ipa工作。省了最后的命令行工作,对新手来说,建议采用Flash Builder 4.6.,下载地址论坛里的兄弟贡献出来了,http://bbs.9ria.com/thread-100284-1-1.html 

下面开始制作ANE文件,

1,启动Xcode,在IOS-》Frameworkd&Library下面,创建一个Cocoa Touch Static Library的项目(我们暂时将项目取名为:CoolExpLibANEIOS)。
2,OK,创建好之后,从你下载的SDK目录下,找到Include文件夹,找到里面的FlashRuntimeExtensions.h,通过项目添加FlashRuntimeExtensions.h文件
3,新建一个Object-C Class文件,此时会产生一个.h一个.m的文件,此时可以把.h文件删除,留一个.m的文件就行了。在这里我们暂定这个M的文件叫CoolExpLibANE.m文件。
4,下面是该文件的内容:我这里定义了两个方法供测试用,一个叫ShowIconBadageNumber,另外一个叫InitNativeCode。其他的代码都是必需的。

  1. #import "FlashRuntimeExtensions.h"
  2. #import <AudioToolbox/AudioServices.h>
  3. #import <UIKit/UIKit.h>
  4. FREObject ShowIconBadageNumber(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]) {
  5.     int32_t t;
  6.     FREGetObjectAsInt32(argv[0], &t);
  7.     
  8.     [[UIApplication sharedApplication] setApplicationIconBadgeNumber:t];
  9.         return argv[0];
  10. }
  11. // InitNativeCode()
  12. //
  13. // An InitNativeCode function is necessary in the Android implementation of this extension.
  14. // Therefore, an analogous function is necessary in the iOS implementation to make
  15. // the ActionScript interface identical for all implementations.
  16. // However, the iOS implementation has nothing to do.
  17. //此方法参考Adobe的官方例子,Vibration的。具体不明白的可以看注释。
  18. FREObject InitNativeCode(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]) {
  19.     
  20.     NSLog(@"Entering InitNativeCode()");
  21.     
  22.     // Nothing to do.
  23.     
  24.     NSLog(@"Exiting InitNativeCode()");
  25.     
  26.     return NULL;
  27. }
  28. // ContextInitializer()
  29. //
  30. // The context initializer is called when the runtime creates the extension context instance.
  31. void ContextInitializer(void* extData, const uint8_t* ctxType, FREContext ctx, 
  32.                                                 uint32_t* numFunctionsToTest, const FRENamedFunction** functionsToSet) {
  33.     
  34.     NSLog(@"Entering ContextInitializer()");
  35.     
  36.         *numFunctionsToTest = 2;
  37.         //这地方 ,如果是两个方法有两个2,如果是三个方法就是3了。当然你可以自己看着定义了。
  38.         FRENamedFunction* func = (FRENamedFunction*) malloc(sizeof(FRENamedFunction) * 2);
  39.         
  40.    //这地方定义方法名
  41.         //Just for consistency with Android
  42.         func[0].name = (const uint8_t*) "initNativeCode";
  43.         func[0].functionData = NULL;
  44.         func[0].function = &InitNativeCode;
  45.     
  46.     //这地方定义方法名
  47.     func[1].name = (const uint8_t*) "ShowIconBadageNumber";
  48.         func[1].functionData = NULL;
  49.     func[1].function = &ShowIconBadageNumber;
  50.         
  51.         *functionsToSet = func;
  52.     
  53.     NSLog(@"Exiting ContextInitializer()");
  54.     
  55. }
  56. // ContextFinalizer()
  57. //
  58. // The context finalizer is called when the extension's ActionScript code
  59. // calls the ExtensionContext instance's dispose() method.
  60. // If the AIR runtime garbage collector disposes of the ExtensionContext instance, the runtime also calls
  61. // ContextFinalizer().
  62. void ContextFinalizer(FREContext ctx) {
  63.     
  64.     NSLog(@"Entering ContextFinalizer()");
  65.     
  66.     // Nothing to clean up.
  67.     
  68.     NSLog(@"Exiting ContextFinalizer()");
  69.     
  70.         return;
  71. }
  72. // ExtInitializer()
  73. //
  74. // The extension initializer is called the first time the ActionScript side of the extension
  75. // calls ExtensionContext.createExtensionContext() for any context.
  76. void ExtInitializer(void** extDataToSet, FREContextInitializer* ctxInitializerToSet, 
  77.                     FREContextFinalizer* ctxFinalizerToSet) {
  78.     
  79.     NSLog(@"Entering ExtInitializer()");
  80.     
  81.     *extDataToSet = NULL;
  82.     *ctxInitializerToSet = &ContextInitializer;
  83.     *ctxFinalizerToSet = &ContextFinalizer;
  84.     
  85.     NSLog(@"Exiting ExtInitializer()");
  86. }
  87. // ExtFinalizer()
  88. //
  89. // The extension finalizer is called when the runtime unloads the extension. However, it is not always called.
  90. void ExtFinalizer(void* extData) {
  91.     
  92.     NSLog(@"Entering ExtFinalizer()");
  93.     
  94.     // Nothing to clean up.
  95.     
  96.     NSLog(@"Exiting ExtFinalizer()");
  97.     return;
  98. }
复制代码

5,Build项目,会在项目文件夹Products生成一个.a 的文件,复制出来,我们在打包ANE的时候要用。

好了。IOS项目创建完毕。下一篇介绍如果创建ActionScript Library。

项目源文件下载在第四篇

posted @ 2014-07-04 10:29  sunminmin2011  阅读(379)  评论(0编辑  收藏  举报