<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Sealyu

    --- 博客已遷移至: http://www.sealyu.com/blog

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      618 隨筆 :: 87 文章 :: 225 評論 :: 0 Trackbacks

    In April 2010 during iOS 4 presentation Steve Jobs announced new advertisement platform – iAd. This platform is intended to allow developers of free apps to earn on their apps showing advertisement. The main idea of iAd is to connect interactivity (using smartphone or tablet) and emotionality of advertisement (JavaScript, HTML5, CSS3, multi-touch). It allows to create absolutely awesome advertisement which looks like apps with video and audio. In addition, iAd ads is opened inside the app and user do not exit his app and can get back anytime he wants.

    Below is the guide on how to add iAd banner into iPhone app. Guide is based on iAd Programming Guide and WWDC 2010 video.

    Add iAd Banner to iPhone App

    The first thing you need to do is to add iAd.framework in your Xcode project which is contained in iOS 4 SDK. Also do not forget to add #import <iAd/iAD.h>. Developers can choose between two banners: 320×50 px for portrait and 480×32 px for landscape. The base of banner is ADBannerView, which is subclass of UIView. So the only thing you have to do is to add this view in your control elements hierarchy (you can do this programmatically or using Interface Builder).

    iOS 4 SDK Interface Builder

    Apple recommends to put banner at the bottom or at the top of the window and do not place it on any moving elements like ScrollView or TableView as far as that will decrease shows of banner (and your revenue respectively) and will make it more difficult for user to tap on the ads.

    Let’s create a new project in Xcode using View-based Application template and add a banner into it. Name of the app – iAdEx. We are going to edit iAdExViewController.h

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #import <UIKit/UIKit.h>;
    #import <iAd/iAd.h>;
     
    @interface iAdExViewController : UIViewController <ADBannerViewDelegate>
    {
          ADBannerView *adView;
          BOOL bannerIsVisible;
    }
    @property (nonatomic,assign) BOOL bannerIsVisible;
    @end

    and modify viewDidLoad method in iAdExViewController.m

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    - (void)viewDidLoad {
          adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
          adView.frame = CGRectOffset(adView.frame, 0, -50);
          adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
          adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
          [self.view addSubview:adView];
          adView.delegate=self;
          self.bannerIsVisible=NO;
          [super viewDidLoad];
    }

    Let’s talk about requiredContentSizeIdentifiers and currentContentSizeIdentifier properties. In the first one you define all types of banners you are going to use. And the second property defines which type of banner you are going to use at the present moment.

    Connection issues

    Banners are downloaded from the network. What if we have no network connection right now? Or Apple has any issues with ads server? Our ADBannerView will be empty is these cases. It doesn’t look very nice and wastes space on the screen. Apple recommends to do it in this way: when there is no banner for any reason remove it from the screen; when banner is received – show it again.

    We have the ADBannerViewDelegate in our class and it can receive messages from banner – bannerViewDidLoadAd (when banner is loaded successfully) and didFailToReceiveAdWithError (when any problems occured). Let’s implement these messages:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    - (void)bannerViewDidLoadAd:(ADBannerView *)banner
    {
     if (!self.bannerIsVisible)
     {
      [UIView beginAnimations:@"animateAdBannerOn" context:NULL];
    // banner is invisible now and moved out of the screen on 50 px
      banner.frame = CGRectOffset(banner.frame, 0, 50);
      [UIView commitAnimations];
      self.bannerIsVisible = YES;
     }
    }
     
    - (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
    {
    if (self.bannerIsVisible)
     {
      [UIView beginAnimations:@"animateAdBannerOff" context:NULL];
    // banner is visible and we move it out of the screen, due to connection issue
      banner.frame = CGRectOffset(banner.frame, 0, -50);
      [UIView commitAnimations];
      self.bannerIsVisible = NO;
     }
    }

    It’s time to launch the app and see what we have now:

    iAd banner on iPhone

    Tap the banner

    iAd Detailed View on iPhone Simulator

    And that’s it – we’ve done it in accordance with Apple’s recommendations. When we launch the app we see the banner. If we tap it, then full view of advertisement is shown. But we still have one issue…

    Stop & Resume your app

    In real app we should stop any application’s activity such as video, audio playback or pause game. In order to solve this task we will create two methods bannerViewActionShouldBegin (when full screen ad is shown) and bannerViewActionDidFinish (when we close ads).

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    - (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
    {
     NSLog(@"Banner view is beginning an ad action");
     BOOL shouldExecuteAction = YES;
     if (!willLeave && shouldExecuteAction)
        {
        // stop all interactive processes in the app
        // [video pause];
        // [audio pause];
        }
     return shouldExecuteAction;
    }
     
    - (void)bannerViewActionDidFinish:(ADBannerView *)banner
    {
       // resume everything you've stopped
       // [video resume];
       // [audio resume];
    }

    Change Orientation of iAd

    What else should we do? We need to make banner change it’s orientation in accordance with iPhone position. First of all we need to change the line where we define types of banners which we will use:

    1
    2
    3
    4
    5
    - (void)viewDidLoad {
    ...
    adView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifier320x50,ADBannerContentSizeIdentifier480x32,nil];
    ...
    }

    and here are methods for changing orientation:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
     // Return YES for supported orientations
     return (interfaceOrientation == UIInterfaceOrientationPortrait|UIInterfaceOrientationPortrait);
    }
     
    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
     if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
      adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier480x32;
     else
      adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
    }

    And here is how it look in a landscape

    iAd banner in landscape mode

    When device changes orientation we change currentContentSizeIdentifier property of ADBannerView. Now we can see banners in landscape mode too. There is still 50 px offset, but it’s easy to fix.

    Apple developers also recommend to write object.delegate=nil; line before deallocating ADBannerView object. In our example it will look like:

    1
    2
    3
    4
    5
    - (void)dealloc {
     adView.delegate=nil;
     [adView release];
     [super dealloc];
    }

    Now we have finished with programming part. Good luck with Apple iAd!

    posted on 2010-11-20 22:58 seal 閱讀(720) 評論(0)  編輯  收藏 所屬分類: iPhone
    主站蜘蛛池模板: 亚洲?V乱码久久精品蜜桃| 亚洲色大18成人网站WWW在线播放 亚洲色大成WWW亚洲女子 | 亚洲一区二区三区高清视频| 你好老叔电影观看免费| 亚洲国产成人VA在线观看| 亚洲成a人片在线观看天堂无码| 思思re热免费精品视频66| 亚洲国产人成在线观看69网站| 国产精品1024在线永久免费| 亚洲Av无码乱码在线znlu| 亚洲色大成网站www永久男同| 无码一区二区三区AV免费| 亚洲成人免费电影| 51精品视频免费国产专区| 亚洲成人午夜在线| 高清一区二区三区免费视频| 亚洲高清国产拍精品26U| 国产又黄又爽又大的免费视频| 久久精品夜色噜噜亚洲A∨| 男女交性无遮挡免费视频| 伊人久久亚洲综合影院| 美女又黄又免费的视频| 亚洲第一视频在线观看免费| 激情婷婷成人亚洲综合| 国产成人免费ā片在线观看| 亚洲AV无码一区二区三区性色| 亚洲精品色在线网站| 日韩精品免费一区二区三区| 国产精品亚洲一区二区三区久久 | 免费人成在线观看播放国产 | 亚洲成Av人片乱码色午夜| 人妻在线日韩免费视频| 亚洲AV永久青草无码精品| 亚洲精品免费在线观看| 精品亚洲aⅴ在线观看| 免费观看黄色的网站| 国产精品亚洲自在线播放页码| 毛片免费在线观看网站| 亚洲精品无码少妇30P| 免费国产高清视频| 精品国产污污免费网站入口|