Recently, we started cautioning publishers against using GADInterstitial’s loadAndDisplayRequest:usingWindow:initialImage: method with mediation . We saw some odd behavior occurring when publishers used that method. This blog post outlines a way to implement the functionality of this method while avoiding the problems that had been associated with it.
The original method replaced the rootViewController
of the window temporarily while the splash image was being shown. Since you have control of your view controllers as the publisher, this step is unnecessary. Assuming you want to show the splash image on app startup, you can place your interstitial code in your rootViewController
’s viewDidLoad:
method. This method is called after the view hierarchy is loaded into memory.
On a high level, all you need to do is initialize a UIImageView and add it into your view hierarchy in the viewDidLoad:
method. To be extra careful, you may also want to hide the status bar while your splash image is being shown.
- (void)viewDidLoad {
[super viewDidLoad];
interstitial_ = [[GADInterstitial alloc] init];
interstitial_.adUnitID = @"MY_INTERSTITIAL_ID";
interstitial_.delegate = self;
// Remember whether the status bar was hidden or not.
hideStatusBar_ = [UIApplication sharedApplication].statusBarHidden;
[UIApplication sharedApplication].statusBarHidden = YES;
GADRequest *request = [GADRequest request];
request.testing = YES;
[interstitial_ loadRequest:request];
// Initialize your splash image and add it to the view.
imageView_ = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"InitialImage"]];
[self.view addSubview:imageView_];
}
If you only want to show the splash image and interstitial the first time the app is loaded, you can write a boolean into NSUserDefaults and check that boolean before you present the interstitial in viewDidLoad:
.
As a final step, you must remove the imageView_ from the view hierarchy once the interstitial comes in. Remember that you also need to remove it if the interstitial fails. To reduce code duplication, you can put this logic into it’s own method.
- (void)restoreController {
[imageView_ removeFromSuperview];
// Restore status bar to state it was before hiding.
[UIApplication sharedApplication].statusBarHidden = hideStatusBar_;
}
#pragma mark GADInterstitialDelegate
- (void)interstitialDidReceiveAd:(GADInterstitial *)ad {
NSLog(@"Received ad successfully");
[interstitial_ presentFromRootViewController:self];
}
- (void)interstitial:(GADInterstitial *)ad
didFailToReceiveAdWithError:(GADRequestError *)error {
NSLog(@"Failed to receive ad with error: %@", [error localizedFailureReason]);
[self restoreController];
}
- (void)interstitialWillDismissScreen:(GADInterstitial *)ad {
// Remove the imageView_ once the interstitial is dismissed.
[self restoreController];
}
If you have any questions about the AdMob SDK, reach out to us on the forum or join us for our upcoming AdMob office hours . You can also follow us on the Google Ads Developer plus page .
- Raj Parameswaran , AdMob Developer Relations