Presenting AdMob Interstitials Before Videos on iOS
There are a number of ways interstitials can be incorporated into applications that play video. One common integration we’ve seen is displaying the interstitial right before a video is played. This blog post will show you how to do this on iOS.
Set Up Your Video Player
For this specific example, we’re going to use MPMoviePlayerController. This class makes it easy to integrate video playback into your iOS application. However, make sure not to use MPMoviePlayerViewController as we want to have control over our own view controller.
We add an observer at this point to tell us when the movie is done playing. This is typically when our view controller will transition away from the MPMoviePlayerController’s view.
Set up a GADInterstitial object as you normally would and and call The trick here is that after you receive and present your interstitial, you’re going to have to add your MPMoviePlayerController’s view into your view hierarchy. You want to make this look seamless so that as the interstitial is dismissed, the movie player looks as though it is in the background. This means making the view hierarchy change in If you’re auto-playing the video, you don’t want to start playing in Remember that you have to unregister for notifications when you are cleaning up your MPMoviePlayerController as well.
Let us know on the forum if you have any questions about interstitials specifically or the Google AdMob SDK in general. You can also follow us on our plus page for AdMob-related updates.
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];
NSURL *movieURL = [bundleURL URLByAppendingPathComponent:@"Video_Name_Here.mp4"];
player_ = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
// Don’t add the player_ into the view hierarchy yet
[player_.view setFrame:self.view.bounds]; // player's frame must match parent's
player_.controlStyle = MPMovieControlStyleFullscreen;
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(moviePlayerPlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player_];
}
Set Up Your Interstitial
loadRequest: at an appropriate time in your application flow. Present the interstitial once it comes back.
- (IBAction)showMovieInterstitial:(id)sender {
self.interstitial = [[[GADInterstitial alloc] init] autorelease];
self.interstitial.delegate = self;
self.interstitial.adUnitID = @”YOUR_IDENTIFIER_HERE”;
[self.interstitial loadRequest: [GADRequest request]];
}
- (void)interstitialDidReceiveAd:(GADInterstitial *)interstitial {
[interstitial presentFromRootViewController:self];
}
interstitialWillDismissScreen: (called before the interstitial is dismissed).
- (void)interstitialWillDismissScreen:(GADInterstitial *)interstitial {
[self.view addSubview:player_.view];
}
interstitialWillDismissScreen: as the user will miss a part of the video when the dismissal transition happens. Instead, you can play the movie in
interstitialDidDismissScreen:.
- (void)interstitialDidDismissScreen:(GADInterstitial *)interstitial {
[player_ play];
}
- (void)dealloc {
if (player_) {
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player_];
[player_.view removeFromSuperview];
[player_ release];
player_ = nil;
}
interstitial_.delegate = nil;
[interstitial_ release];
[super dealloc];
}