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:

@interface ViewController : UIViewController <ADInterstitialAdDelegate>
-(void)showFullScreenAd;
@end

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:

@implementation ViewController {
    ADInterstitialAd* _interstitial;
    BOOL _requestingAd;
}

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

#pragma mark Interstitial Ad

-(void)showFullScreenAd {
    if (_requestingAd == NO) {
        _interstitial = [[ADInterstitialAd alloc] init];
        _interstitial.delegate = self;
        self.interstitialPresentationPolicy = ADInterstitialPresentationPolicyManual;
        NSLog(@"Ad Request");
        _requestingAd = YES;
    }

}

-(void)interstitialAd:(ADInterstitialAd *)interstitialAd didFailWithError:(NSError *)error {
    _interstitial = nil;
    _requestingAd = NO;
    NSLog(@"Ad didFailWithERROR");
    NSLog(@"%@", error);
}

-(void)interstitialAdDidLoad:(ADInterstitialAd *)interstitialAd {
    NSLog(@"Ad DidLOAD");
    if (interstitialAd != nil && _interstitial != nil && _requestingAd == YES) {
        [_interstitial presentInView:self.view];
        _requestingAd = NO;
    }
}

-(void)interstitialAdDidUnload:(ADInterstitialAd *)interstitialAd {
    _interstitial = nil;
    _requestingAd = NO;
    NSLog(@"Ad DidUNLOAD");
}

-(void)interstitialAdActionDidFinish:(ADInterstitialAd *)interstitialAd {
    _interstitial = nil;
    _requestingAd = NO;
    NSLog(@"Ad DidFINISH");
}

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:

#pragma mark Interstitial Ad

-(void)showFullScreenAd {
    if (_requestingAd == NO) {
        _interstitial = [[ADInterstitialAd alloc] init];
        _interstitial.delegate = self;
        NSLog(@"Ad Request");
        _requestingAd = YES;
    }
    
}

-(void)interstitialAd:(ADInterstitialAd *)interstitialAd didFailWithError:(NSError *)error {
    _requestingAd = NO;
    NSLog(@"Ad didFailWithERROR");
    NSLog(@"%@", error);
}

-(void)interstitialAdDidLoad:(ADInterstitialAd *)interstitialAd {
    NSLog(@"Ad DidLOAD");
    if (interstitialAd.loaded) {

        CGRect interstitialFrame = self.view.bounds;
        interstitialFrame.origin = CGPointMake(0, 0);
        _adView = [[UIView alloc] initWithFrame:interstitialFrame];
        [self.view addSubview:_adView];
        
        [_interstitial presentInView:_adView];

        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self action:@selector(closeAd:) forControlEvents:UIControlEventTouchDown];
        button.backgroundColor = [UIColor clearColor];
        [button setBackgroundImage:[UIImage imageNamed:@"close-button.png"] forState:UIControlStateNormal];
        button.frame = CGRectMake(0, 0, 30, 30);
        [_adView addSubview:button];
        
        [UIView beginAnimations:@"animateAdBannerOn" context:nil];
        [UIView setAnimationDuration:1];
        [_adView setAlpha:1];
        [UIView commitAnimations];

    }
}

-(void)closeAd:(id)sender {
    [UIView beginAnimations:@"animateAdBannerOff" context:nil];
    [UIView setAnimationDuration:1];
    [_adView setAlpha:0];
    [UIView commitAnimations];
    
    _adView=nil;
    _requestingAd = NO;
}

-(void)interstitialAdDidUnload:(ADInterstitialAd *)interstitialAd {
    _requestingAd = NO;
    NSLog(@"Ad DidUNLOAD");
}

-(void)interstitialAdActionDidFinish:(ADInterstitialAd *)interstitialAd {
    _requestingAd = NO;
    NSLog(@"Ad DidFINISH");
}

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:

-(void)showFullScreenAd {
    self.interstitialPresentationPolicy = ADInterstitialPresentationPolicyManual;
    BOOL canPresent= [self requestInterstitialAdPresentation];
}

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.

References

https://developer.apple.com/library/ios/documentation/iAd/Reference/ADInterstitialAd_Ref/Introduction/Introduction.html

https://developer.apple.com/library/ios/documentation/iAd/Reference/UIViewController_iAd_Additions/Reference/Reference.html

https://developer.apple.com/library/ios/documentation/userexperience/conceptual/iAd_Guide/Full-ScreenAdvertisements/Full-ScreenAdvertisements.html