Looking for good programming challenges?

Use the search below to find our solutions for selected questions!

Interstitial ads iPhone using the iAd framework

Sharing is caring!

In this tutorial I will briefly explain several methods for adding interstitial ads iPhone in your iOS 7 App using the iAd framework. The example I will be working on is a small game implemented in Apple’s SpriteKit framework.

The tutorial assumes that you have already created a new App in iTunes connect and have already enabled iAd Advertising.

Using the old API

Using the old specification the ADInterstitialAdDelegate protocol must be implemented, lets say by a UIViewController subclass so that your application can respond to changes in an ADInterstitialAd object. An ad object calls its delegate whenever the state of the ad changes.

I will implement this protocol in my ViewController that controls the views/scenes of my game. So the ViewController.h file will look like follows:

In addition I have added a method -(void)showFullScreenAd that can be called from outside the ViewController anytime we want to display a new ad. A typical scenario in my game goes like so:
– The user starts playing the game
– The user either wins or looses
– The GameOver scene gets displayed

So I want my ads to be displayed just before the GameOver scene gets presented. So a typical scenario would be to call the ViewController’s -(void)showFullScreenAd after the GameOver’s scene content gets created.

In my ViewController.m file I have added the following:

and for the protocol and interface implementation the following methods have been implemented:

So every time I call -(void)showFullScreenAd, a new ADInterstitialAd gets created. After the ad object is created, it automatically downloads an advertisement from the iAd App Network. When the ad object finishes downloading the advertisement, it signals your delegate. Your app takes the loaded ad and presents it to the user. The actual process your app uses to present the advertisement varies depending on whether you want to present it modally or as a modeless page.

In this tutorial I will present the Ad as a Page of Content mainly for two reasons:
1. Presenting an Ad Modally using [_interstitial presentFromViewController:self] is deprecated since iOS 7.
2. Presenting an Ad Modally using [_interstitial presentFromViewController:self] doesn’t take into consideration the device orientation. It always displays the add in portrait mode.

So again, I will present my ad as a Page of Content by calling the ad’s presentInView: method as soon as the Ad is loaded.
The good thing about this method is that device orientation is taken into consideration. The downside is that the presented ad doesn’t have a close button and so the user needs to terminate the entire app to get rid of the Ad. I don’t know why the iAd framework would provide something in such a state.

So here is the delegate implementation that also adds a close button and some fancy animations for showing and hiding the ad:

Using the new API

The new API doesn’t require the call of presentInView: or presentFromViewController:. It also does not require the implementation of the ADInterstitialAdDelegate protocol. All we need to do is to call the following implementation of the showFullScreenAd method:

The downside is that the device orientation isn’t taken into account and the ad is always displayed in portrait mode. Also we have no control what so ever what happened with the Ad (finish loading, etc) since no delegate implementation is being done.