通过AIR Native Extension在AIR应用中加入iAd广告(一) —— Flash Builder篇

 

目前通过Adobe AIR发布iOS应用的案例已经很多了. 随着AIR runtime及AIR SDK的不断升级,AIR App for iOS在iDevice运行流畅度大大提高.感谢Adobe带给大家这么好的技术和工具.

在苹果商店中, 可直接发布付费应用获得收益, 也可免费发放并通过广告得到分成. 但AIR不支持原生iAd广告组件.这时只能通过ANE(Air Native Extension)调用苹果原生API. 官方ANE插件站推荐了iOS iAd Native Extension for Adobe AIR插件. 最近无意中在Google Code找到iad-air-ios,重要的是, 它是免费的 :)

下面通过Flash Builder 4.6介绍下使用方法.

1. 打开FB4.6, 新建ActionSript Mobile Project, 本例中使用iAdTest为项目名.去掉黑梅和安卓的选项, 只勾选iOS系统:

 

2. 前往iad-air-ios, 下载最新的插件 iAdPrefixed.zip 并解压.

3.打开iAdTest项目属性,选择ActionScript Build Path, 切换到Native Extensions, 选择第2步解压的ANE文件并添加到项目.

 

4. 敲入主程序代码:

package
{
    import com.adobe.nativeExtensions.AdBanner;
    import com.adobe.nativeExtensions.AdBannerEvent;
    import com.adobe.nativeExtensions.AdBannerPosition;
 
    import flash.display.Sprite;
    import flash.display.Stage;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
 
    public class iAdTest extends Sprite
    {
        public function iAdTest()
        {
            super();
 
            this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
        }
 
        protected function addedToStageHandler(event:Event):void
        {
            this.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
 
            initAD();
        }
 
        private function initAD():void
        {
            AdBanner.adView.startShowingAds(AdBannerPosition.TOP, false);  
            AdBanner.adView.addEventListener(AdBannerEvent.AD_LOADED, adLoaded);  
            AdBanner.adView.addEventListener(AdBannerEvent.AD_LOADING_FAILED, adFailed)
            AdBanner.adView.addEventListener(AdBannerEvent.USER_INTERACTION_STARTING, adStart);  
            AdBanner.adView.addEventListener(AdBannerEvent.USER_INTERACTION_FINISHED, adFinished); 
        }
 
        private function adLoaded(event:AdBannerEvent):void
        {
            trace("Ad Loaded");         
            AdBanner.adView.visible = true;         
            trace("leaving: " + event.leaving); 
        }
 
        private function adFailed(event:AdBannerEvent):void
        {
            trace("Loaded Failded: " + event.errorCode);
            AdBanner.adView.visible = false;
        }
 
        private function adStart(event:AdBannerEvent):void
        {
            trace("Starting");
            trace(stage.orientation);
        }
 
        private function adFinished(event:AdBannerEvent):void
        {
            trace("Finished");
            trace(stage.orientation);
        }
    }
}

5.为配合从网络下载的证书, 要打开iAdTest-app.xml修改id:
<!– A universally unique application identifier. Must be unique across all AIR applications.
    Using a reverse DNS-style name as the id is recommended. (Eg. com.example.ExampleApplication.) Required.–>
<id>com.gamevil.zenonia2</id>
6.发布程序

 

6. 选择证书(网络下载的测试证书)

 

7. 如果没有意外情况发生, 等待数分钟便可发布完成.
连接iPhone(已越狱),将生成的iAdTest.ipa安装并运行,稍等片刻就可以看到左边的界面了.
运行截图:

 

程序源代码,证书(密码1234): 点击此处下载

 

20 comments to 通过AIR Native Extension在AIR应用中加入iAd广告(一) —— Flash Builder篇

  • nihao

    为什么会报这样的错
    Loaded Failded: 6

    [Reply]

  • cxh612

    你好,为什么会报错。
    TypeError: Error #1009: Cannot access a property or method of a null object reference.

    at com.adobe.nativeExtensions::AdBanner()[/Users/adobe/Projects/iAd/iAd/src/com/adobe/nativeExtensions/AdBanner.as:44]
    at com.adobe.nativeExtensions::AdBanner$/get adView()[/Users/adobe/Projects/iAd/iAd/src/com/adobe/nativeExtensions/AdBanner.as:52]
    at iAdTest/initAD()[/Users/jay/Documents/Adobe Flash Builder 4.7/IadTest/src/iAdTest.as:33]
    at iAdTest/addedToStageHandler()[/Users/jay/Documents/Adobe Flash Builder 4.7/IadTest/src/iAdTest.as:28]
    at runtime::ContentPlayer/loadInitialContent()
    at runtime::ContentPlayer/playRawContent()
    at runtime::ContentPlayer/playContent()
    at runtime::AppRunner/run()
    at ADLAppEntry/run()
    at global/runtime::ADLEntry()

    [Reply]

  • You have observed very interesting points! ps nice internet site. “Hares can gamble over the body of a dead lion.” by Publilius Syrus.

    [Reply]

  • Nice post. I used to be checking continuously this blog and I’m impressed! Very helpful info particularly the closing part :) I care for such information much. I was looking for this certain info for a very long time. Thank you and good luck.

    [Reply]

  • Thanks for the sensible critique. Me & my friend were just preparing to do a little research about this. We grabbed a book from our area library but I think I learned more from this post. I’m very glad to see such great info being shared freely out there…

    [Reply]

  • I hope you are planning to write more articles like this. I want to read more on your thoughts since they are much like mine.

    [Reply]

  • “Appreciate you sharing, great post. Fantastic.”

    [Reply]

  • You’ve made some good points there. I looked on the net for additional information about the issue and found most individuals will go along with your views on this website.

    [Reply]

  • naturally like your web-site however you have to take a look at the spelling on quite a few of your posts. Several of them are rife with spelling problems and I find it very troublesome to inform the reality then again I¡¦ll surely come back again.

    [Reply]

  • change

    I download source and export ipa install my iphone for ios5.1 ,The screen appears a black and white,How can I do?

    [Reply]

    kingnare Reply:

    Debug with FB, have a look at the info of console :)

    [Reply]

  • change

    I download source and export ipa install iphone ios5.1 can’t work ,how can I do?

    [Reply]

  • You’re so cool! I don’t suppose I’ve read something like that before. So good to find someone with original thoughts on this topic. Seriously.. thank you for starting this up. This web site is something that’s needed on the internet, someone with a little originality!

    [Reply]

  • vince

    已经测试成功,不知道如何放自己要的广告呢。。通过AdBanner.stream AdBanner.file 这两个接口吗?

    [Reply]

    kingnare Reply:

    应该是不需要了, 会使用程序证书.
    那两个接口我没有用, 是可以自己指定广告吧 :)

    [Reply]

  • vince

    hi, 测试同jack123 错误。
    Loaded Failded: 6
    设备版本: touch 4 ios版本:5.0.1,

    [Reply]

  • jack123

    下载了源代码,部署到iphone之后,运行,显示白屏幕。调试之后,这行报错trace(“Loaded Failded: ” + event.errorCode);

    提示: Loaded Failded: 6

    请问是什么问题

    [Reply]

    kingnare Reply:

    // There are some bug prior to iOS 4.3 which cause some positioning glitches of the
    // banner. Please see native code to change the implementation if you know some way
    public static const ERROR_UNSUPPOERTED_OS:int = 6;

    源码中有这么一句话.我测试用的是iOS5

    [Reply]

  • dzq

    Hi 老兄, iAd_by_FB.zip貌似有有点问题啊,打不开啊

    [Reply]

    kingnare Reply:

    多谢杜哥提醒,已经重新上传了 :)

    [Reply]

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="">