Picture of Brian Love wearing black against a dark wall in Portland, OR.

Brian Love

How to show Interstitial Ads on iPad apps

We’ve all seen an interstitial ad when playing a free game on our iPad or iPhone.

It’s the ad that pops up before or after playing or displaying of content. It usually takes up the entire screen when being shown. And, often you cannot hide or dismiss the advertisement until after a couple of seconds.

This is a great way to provide advertising revenue steams to your app. With the introduction of iAd, we have been able to easily embed advertisements in the form of banners within our applications. You can also display these interstitial ads to your iPad (not iPhone) users using the iAd network.

ADInterstitialAd

This is the class that you are going to be using to create the interstitial ads in your iPad application. To get started we are going to define a class instance variable within our controller to contain reference to a new instance of the ADInterstitialAd.

@interface MyViewController : UIViewController <ADInterstitialAdDelegate>
@end

@implementation MyViewController {
  ADInterstitialAd *interstitial;
}

//load up the interstitial ad
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
  interstitial = [[ADInterstitialAd alloc] init];
  interstitial.delegate = self;
  }
}

You can see in the code above that I have declared the variable interstitial, and then allocated and initialized an instance of ADInterstitialAd. I do this in my viewDidLoad method so that the ad is loaded and ready to display at the end of the round.

Notice that I have put the allocation and initialization code within an if statement to check if I am on an iPad. You have to do this, otherwise you will get an exception when you attempt to create the interstitial ad on an iPhone or iPod.

Lastly, I also set the delegate reference to the view controller (self). This is because my view controller will implement the ADInterstitialAdDelegate protocol. So, these methods will be called on the view controller because I am setting the delegate as (my)self.

Showing the Interstitial Ad

At the end of the game, I am going to simply call a method within my view controller to show the Interstitial Ad:

- (IBAction)endGame:(UIButton *)sender {
  [self showInterstitialAd];
}

- (void)showInterstitialAd {
  [self performSegueWithIdentifier:segueToSummaryView sender:self];
  if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad && interstitial.loaded){
    [interstitial presentFromViewController:self];
  }
}

In the code above, I am going to first perform my regular segue to the next screen. In my instance, I am showing an end game screen that summarizes the user’s performance. I want to go ahead and show this screen to the user, whether or not they are on an iPhone or an iPad. Then, if the user is on an iPad, I want to have a modal view slide up and show the interstitial ad to the user. After they dismiss the ad, the game summary view is waiting for them.

ADInterstitialAdDelegate

There are two required methods for this protocol: interstitialAdDidUnload: and interstitialAd:didFailWithErrors:. We are going to implement these methods, but I don’t have any action that I need to perform right now if the ad unloads or fails, so I simply log out a message to my console.

#pragma mark Interstitial Ad

- (void)interstitialAdDidLoad:(ADInterstitialAd *)interstitialAd {
  NSLog(@"Interstitial ad loaded");
}

- (void)interstitialAdDidUnload:(ADInterstitialAd *)interstitialAd {
  interstitial = nil;
}

- (void)interstitialAd:(ADInterstitialAd *)interstitialAd didFailWithError:(NSError *)error {
  NSLog(@"Interstitial ad failed to load");
}

That’s it! Now, you can start using interstitial iAds in your iPad apps.