Interstitial Integration For iOS

blogvinskiy edited this page Oct 15, 2014 · 12 revisions

Interstitial Integration for iOS

Interstitial ads provide full-screen experiences, commonly incorporating rich media to offer a higher level of interactivity than banner ads. Interstitials are typically shown during natural transitions in your application, such as on app launch, after completing a game level, or while your app is downloading content from the internet. You can use the (MPInterstitialAdController) and its delegate callbacks to fetch and display interstitial ads in your app.

Prerequisites:

Before integrating interstitial ads in your app, you’ll need to go through the steps in our Getting Started Guide to create an account on MoPub and integrate the SDK into your project.

Basic integration

  1. In your view controller's header file:
    • Import the MPInterstitialAdController.h header file and declare an MPInterstitialAdController *interstitial property.
    • Declare that your view controller implements the MPInterstitialAdControllerDelegate protocol.
  2. In your view controller's implementation file, instantiate an MPInterstitialAdController using the class convenience method +interstitialAdControllerForAdUnitId:, passing in your ad unit ID.
  3. Register your view controller as the interstitial's delegate. (e.g. self.interstitial.delegate = self;)
  4. Pre-fetch the interstitial ad by calling -loadAd on the interstitial.
  5. When you'd like to display the ad, check the ad's ready property.
  6. If the ad is ready to be shown, call -showFromViewController: on the interstitial, passing in your view controller.

The following code snippets demonstrate the above steps in the context of a game application; specifically, how to pre-fetch an interstitial and display it after a level has ended.

IMPORTANT: If you are using MRC you'll need to set the -fobjc-arc compiler flag on these files. Follow the instructions here.

// MyViewController.h

#import "MPInterstitialAdController.h"

@interface MyViewController : UIViewController <MPInterstitialAdControllerDelegate>

@property (nonatomic, retain) MPInterstitialAdController *interstitial;

@end
// MyViewController.m

#import "MyViewController.h"

@implementation MyViewController

// Our loadView implementation will pre-fetch our interstitial ad.
- (void)loadView {
    // ... your other -loadView code ...
    [self loadInterstitial];
}

- (void)loadInterstitial {
    // Instantiate the interstitial using the class convenience method.
    self.interstitial = [MPInterstitialAdController
        interstitialAdControllerForAdUnitId:@"<YOUR_ADUNIT_ID_HERE>"];

    // Fetch the interstitial ad.
    [self.interstitial loadAd];
}

// Present the ad only after it is ready.
- (void)levelDidEnd {
    if (self.interstitial.ready) [self.interstitial showFromViewController:self];
    else {
        // The interstitial wasn't ready, so continue as usual.
    }
}

Receiving optional delegate callbacks

MPInterstitialAdControllerDelegate includes a variety of optional callbacks tthat you can use to be notified of events, e.g. when an interstitial has successfully loaded, or when an interstitial is about to appear. Check out the MPInterstitialAdControllerDelegate in (MPInterstitialAdController.h)[https://github.com/mopub/mopub-ios-sdk/blob/master/MoPubSDK/MPInterstitialAdController.h] for a list of these methods.

Example 1 (Pre-fetching): You can be notified that an interstitial was fetched successfully by implementing -interstitialDidLoadAd:.

Example 2 (Presentation/dismissal callbacks): Suppose that your application is a game. You'd like to pause the game whenever you present an interstitial, and resume it when the interstitial is dismissed. You can accomplish this using the optional -interstitialWillAppear: and -interstitialDidDisappear delegate callbacks:

- (void)interstitialWillAppear:(MPInterstitialAdController *)interstitial {
    [self pauseGame];
}

- (void)interstitialDidDisappear:(MPInterstitialAdController *)interstitial {
    [self resumeGame];
}