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);

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.
#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];
...
}
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.




Help





