iAd Integration

Advertisements are another option to provide your iOS applications still for free but also to get some cash back for your work. Although ads are not that popular to app users Apple made a big hit with their new iAd network. I’d like to call iAd the next level of advertisement on mobile devices because they offer a totally different experience compared to common text or banner based ads.

First steps to enable iAd are:

  1. Go to iTunes Connect and fill out all necessary information on the iAd Network contract (It will take some time until Apple confirms your contract).
  2. Setup your application information at iTunes Connect as usual.
  3. Go to the iAd network settings of your application and enable the iAd network and choose your primary target group.

After that go straight to your Xcode project and check out following things:

  1. View your project info and add a weak (iOS < 4.0 doesn’t support iAd) reference to the iAd framework.
  2. Go to your application delegate interface and make it implement the ADBannerViewDelegate  protocol.
  3. Add a simple variable to observe the iAd visibility BOOL iAdVisible .
  4. Go to your application delegate implementation and setup your iAd banner view. To check if iAd is available on the current run-time environment I implemented a simple category for the UIDevice  class.
- (void) setupiAd
{
    if([UIDevice isIAdAvailable] == NO)
        return;

    // Initialize a new iAd banner view
    ADBannerView *adView = [[ADBannerView alloc] initWithFrame:CGRectZero];

    // Set the delegate and hide the view initially
    adView.delegate     = self;
    adView.hidden       = YES;

    // Different iOS versions support different view sizes
    // but we just wanna add a portrait ad view
    if(&amp;ADBannerContentSizeIdentifierPortrait != nil)
        // Size identifier for IPAD + IPHONE
        adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    else
        // Size identifier only for IPHONE
        adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;

    // Move the banner view to the background (across the screen top)
    adView.frame    = CGRectOffset(adView.frame, 0, -adView.frame.size.height);
    adView.hidden   = NO;

    // Add the banner to the current view
    [viewController.view addSubview:adView];
    [adView release];

    // Set status of iAd banner
    iAdVisible = NO;
}

Now you are almost done. Call setupiAd  within your applicationDidFinishLaunching:  method and this will add an iAd banner view to your current view if iAd is available. The last step is to implement the ADBannerViewDelegate  methods and to simply show the banner view whenever ad content is available and hide the banner view when no ad content is available or some kind of error occurred. It is very important to hide the banner once an error occurred and to show the banner if content is given. The iAd framework will stop providing you with ad content if you don’t act properly. Some helper methods to hide/show banner view:

- (void) hideBannerView:(ADBannerView*)banner
{
    if (!iAdVisible)
        return;

    [UIView beginAnimations:@"animateAdBannerHide" context:NULL];

    // Assumes the banner view is placed at the top of the screen.
    banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
    [UIView commitAnimations];

    // Update visibility state
    iAdVisible = NO;
}

- (void) showBannerView:(ADBannerView*)banner
{
    if (iAdVisible)
        return;

    [UIView beginAnimations:@"animateAdBannerShow" context:NULL];

    // Assumes the banner view is just across the top of the screen.
    banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
    [UIView commitAnimations];

    // Update visibility state
    iAdVisible = YES;
}

Implementation of the ADBannerViewDelegate  methods:

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    // Ad content available -&gt; show banner
    [self showBannerView:banner];
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    // In any error case hide the banner
    [self hideBannerView:banner];

    NSLog(@"ADBannerView didFailToReceiveAdWithError: %d, %@, %@",
          [error code],
          [error domain],
          [error localizedDescription]);
}

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
    // Save your application state here
    return YES;
}

- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
    // Restore your application state here
}

That’s it! Start your application with your simulator and you should see an empty view with an iAd banner view and some test content in it:

post_iad_showcase

If you watch the simulator awhile you will notice that sometimes the banner view disappears and your debugger console will show up a message like ADBannerView didFailToReceiveAdWithError: 3, ADErrorDomain, The operation couldn’t be completed. Ad inventory unavailable . This error messages are intended by Apple so you can test whether your implementation works correctly or not.

Leave a Reply

Your email address will not be published. Required fields are marked *