OCT 18, 2010

iAd in 3 simple steps

For more than a year, I’ve worked on the iOS port of iDaft without making a single penny. That’s until Apple announced iAd back in April.

There are multiple success stories about developers who made (and are still making) a fortune with iAd, but that’s another story. Today we just want to quickly get iAds up and running in your app.

1. Add iAd.framework

Double click your app target and add the iAd framework. If you care about iOS 3 compatibility, make sure it’s weak-linked.

2. Add the ADBannerView

ADBanner views need to be displayed inside a UIViewController since ad content is presented modally. You can add the ADBannerView to your view hierarchy using Interface Builder. However, to stay iOS 3 compatible, you must add the banner programatically after first checking if the symbol exists.

if([ADBannerView respondsToSelector:@selector(class)]) {
	adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
	adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
	adView.delegate = self;
	[self.view addSubview:adView];

3. Implement the ADBannerViewDelegate

If no ad is available (which is the common case), you must hide the banner view (using setAlpha, setHidden, etc). ADBannerViewDelegate offers 2 methods for handling that:

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error

And 2 other methods for handling user interactions:

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
- (void)bannerViewActionDidFinish:(ADBannerView *)banner

That’s it. By now you should have test advertisements showing up in your app. To learn more about iAd and how it works, you should definitely have a look at Apple’s iAd Programming Guide.

A couple of notes about iAd