Today has involved me launching Privacy for Facebook every 10 minutes, checking to see if iAd has rolled out to the app. Never in my life have I been so excited to see advertising before.
Anyway, “in honor” of iAd rolling out live today, I thought I’d give a quick walkthrough on how to implement iAd correctly.
“But James,” my lone reader asks themselves, “is it even possible in implement iAd incorrectly?” Certainly, dear reader. In fact, Apple made it very easy to do so. See, the iAd banner class (ADBannerView) is right there in the Interface Builder library, so a naive programmer (like me) might just drag that into the interface, rebuild, code sign, deploy.

Well, if you went that route, you’d very likely receive a nice little email from appreview, that, in part, says the following:
“We noticed that your app, Privacy for Facebook, is displaying an empty iAd banner when ad content is not available. The banner within the app should be hidden whenever ad content is not being served by iAd.”
Basically, displaying a blank banner is a no-no. There are a couple reasons why banners may not be displayed: they haven’t rolled out or aren’t available in a particular locale, there is no ad inventory available for your specific app, or there could just be a network issue. Apple wants you to make sure you never show blank banners.

So here’s a quick and dirty way of doing it correctly (assuming a portrait orientation, landscape is left as an exercise for the reader):
- Add an ADBannerView to your interface, but just off screen. In this example, the ad should be at the bottom of the interface, so the origin of the ADBannerView is 0,460.
- Set the view controller as the ADBannerView’s delegate.
- Make sure you’ve included the iAd framework in the view controller’s .h file:
#import <iAd/iAd.h>
- Have the view controller implement ADBannerViewDelegate, and add a bool bannerIsVisible; to the interface
- Add methods like the following to the view controller’s implementation. Note these methods animate the banner up from the bottom, while at the same time resizing my other views (buttonFrame and web).
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!bannerIsVisible)
{
NSLog(@"bannerViewDidLoadAd");
[UIView beginAnimations:@"animateAdBannerOn" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0, -50);
buttonFrame.frame = CGRectOffset(buttonFrame.frame, 0, -50);
web.frame = CGRectMake(web.frame.origin.x,
web.frame.origin.y,
web.frame.size.width,
web.frame.size.height-50);
[UIView commitAnimations];
bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (bannerIsVisible)
{
NSLog(@"bannerView:didFailToReceiveAdWithError:");
[UIView beginAnimations:@"animateAdBannerOff" context:NULL];
// assumes the banner view is at the top of the screen.
banner.frame = CGRectOffset(banner.frame, 0, 50);
buttonFrame.frame = CGRectOffset(buttonFrame.frame, 0, 50);
web.frame = CGRectMake(web.frame.origin.x,
web.frame.origin.y,
web.frame.size.width,
web.frame.size.height+50);
[UIView commitAnimations];
bannerIsVisible = NO;
}
}
- Test it. Note that in testing mode, Apple will occasionally return an error instead of the Test Ad, so you can make sure your logic is working correctly.
Enjoy!