Adding an iAd advertisement to your iPhone app

With the introduction if iOS 4, Apple have also introduced iAds, a mobile advertising platform that aims to convey a new concept of interactive marketing on your smart-device. Through CSS3 and multi-touching, you will get a WebKit-powered framework to deliver optimal user experience right within your app. This layer in fact sits on top of your application, so it won’t use your app-reserved memory, as well as not leaving the app when the user interacts with the ad, but rather displayed on top of it. 

Putting Ads in your app

The ADBannerView is the core class within this framework. which is simply a view and should be part of the view controller, that you have in your app. This class manages and retrieves and displays the ads from the iAd network, managing the user interaction. The only thing you need to worry about is:

  • placement of the banner within your view controller;
  • respond to networking issues (this will be discussed later);

So in Interface Builder, you drag onto your view controller, the Ad BannerView, the same way you would add any other view in IB. In XCode, you make sure to add the iAd framework to your list of frameworks. 

Posted Image

Working with the Banner view lifecycle

OK, so you have the banner, the next thing to concern yourself with is the managing of connectivity and various inventory changes that might occur throughout the user experience. That is, the banner queries the iAd network for a new ad (inventory content), and this depends on connectivity. ADBannerView may not always have content in it, because you may not have any network signal on your iPhone, you may have your phone in Airplane mode. 

So the two lifecycle states you have is managed through the ADBannerViewDelegate callbacks:

  • bannerViewDidLoadAd - Has ad content.
  • bannerView:didFailToReceiveAdWithError - When you have network issues and it failed to load the ad during it’s cycle. You also may not have any inventory because of how set up your ad targeting, where there is no ads based on your filter settings.

So, if we do get a failure, which is not a real error, but just something to let you know that you perhaps would not want to have an empty box, allowing you to hide (place offscreen) and back on screen when you do have content. Space is prime real-estate in the mobile world. An example of how to do this:

</span></span></span></span> <pre>#pragma mark - #pragma mark ADBannerViewDelegate methods

(void)bannerView:(ADBannerView *)bannerdidFailToReceiveAdWithError: (NSError *)error {   //hide banner   [self moveBannerFromScreen]; }

(void)bannerViewbannerdidLoadAd: (AdBannerView *)banner {   //show when we have content   [self moveBannerBackOn]; } (void)moveBannerBackOn {   //bring back to frame   CGRect theBannerFrame = self.bannerView.frame;   theBannerFrame.origin.y = self.view.frame.size.height - theBannerFrame.size.height;

 //shrink existing table  CGRect originalTableFrame = self.tableView.frame;  CGFloat newTableHeight = self.view.frame.size.height -  theBannerFrame.size.height;  CGRect newTableFrame = originalTableFrame;  newTableFrame.size.height = newTableHeight;

  self.tableView.frame = newTableFrame; //you may want to animate this   self.bannerView.frame = theBannerFrame;


(void)viewDidLoadAd { ...   //hide at start until you know you get ad   [self moveBannerFromScreen]; ... }</pre>

Responding to actions

OK now you are seeing the ad. The ideal thing to do is reduce activity in your app (pause action when the ad is showing), and to ensure the best experience for the user when viewing the ad. Also you should save minimal state (i.e the tab you are on, rather than content), and when the ad is complete, resume the ap.

In ADBannerViewDelegate, when a user action begins on the ad (you pausing your app) you get the following callback:

- (BOOL) bannerViewActionShouldBegin: (ADBannerView *)banner willLeaveApplication: (BOOL)willLeave 
        //pause your app here
        return yes;

 //When the user comes back you get the callback:
- (BOOL) bannerViewActionDidFinish: (ADBannerView *)banner 
        //resume your app here
        return yes;

And there you have it. For more on iAds and the iAd framework, please visit the Apple Developer Centre for documentation and sample code.


Doron KatziPhone Dev