星期四, 4月 26, 2012

使用 Air Native Extension(ANE) 在 Air Mobile App


Air SDK 好像是到了 3.0 之後的版本都可以使用 Air Native Extension(ANE)
網路上有些神人們寫好且免費的 ANE 可以使用 以 iOS 來說就用過 iNativeAlert, AdMob iOS, Vibration, NetworkInfo 等
可以到 http://www.adobe.com/devnet/air/native-extensions-for-air.html 或是 http://extensionsforair.com/ 找找

紀錄一下怎麼使用 ANE 以 iOS Maps (http://code.google.com/p/air-maps-ane/) 當範例
1. 下載 ANE 主要要有 2 個檔 airmaps.ane & iMaps.swc
2. airmaps.ane 就在專案下開個目錄 ext 放到裡面吧
3. 在專案中把 iMaps.swc 加到元件庫 並設定外部元件庫 (就是編譯不包進去)

4. 設定 ANE 進 application.xml 裡
<extensions>
 <extensionID>com.sampleNativeExtensions.Maps</extensionID>
</extensions>

5. 寫 CODE
private var googleMap:Map;  
public function iMapTest() {   
 var sp:Sprite = new Sprite();
 sp.graphics.beginFill(0xDD0000);
 sp.graphics.drawCircle(0, 0, 40);
 sp.graphics.endFill();
 addChild(sp);
 sp.x = stage.stageWidth >> 1;
 sp.y = 40;
 
 var viewPortMap:Rectangle = new Rectangle(0, 80, 320, 400);
 googleMap = new Map();
 googleMap.viewPort = viewPortMap;
 
 googleMap.setSize(new Point(320, 400));
 googleMap.setMapType(0);
 googleMap.setZoom(17);  
 googleMap.setCenter(new LatLng(25.18446, 121.7515));
 googleMap.visible = true;
 
 var marker:Marker = new Marker(new LatLng(25.18446, 121.7515));
 marker.title = "TEST";
 marker.subtitle = "AWESOME!!";
 googleMap.addOverlay(marker);
}

6. Build 專案 產出 SWF 檔 ((有用ANE好像都沒法在 Air Player 上先試跑...))
7. 編譯 IPA 檔 這邊比較特別的是 因為使用 ANE 原本的編譯指令後面要加上 -extdir 放置 .ane 檔目錄 一般情況的 ANE 應該這樣就可以產出 IPA 檔 不過 iOS Maps 這個 ANE 有用到 iOS SDK 的 MapKit 所以得再加上 -platformsdk iOS SDK 的路徑 也就是說如果要用這 ANE 必須在 Mac 電腦下執行... 在 Mac 下開啟終端機 輸入

adt 
-package
-target ipa-{看要包哪種形式}
-storetype pkcs12
-provisioning-profile {MOBILE_PROVISION 檔案位置}
-keystore {P12 檔案位置}
-storepass {P12 檔密碼}
iOSNativeMaps.ipa
application.xml
bin/iOSNativeMaps.swf
-extdir ext
-platformsdk /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk

很奇怪 我生成時有 warning 不過 IPA 還是產生出來了


8. 安裝到手機上看看 OK出來了~


使用原生的地圖效能真的比較好 不過這個 ANE 目前提供的 API 還不是很多 應該還是夠用... 如果嫌這 ANE 功能不足的話 就還是得用 StageWebView 來解決 然後再搭配 StageWebViewBridge 功能會更齊全

沒有留言: