Search code examples
iosuiviewcontrolleradmobinterstitial

GADInterstitial presentFromRootViewController causes current View Controller to close


I'm attempting to show a GADInterstitial ad when a user of my app clicks a certain button to go to a certain ViewController. In the new ViewController's viewDidLoad method I check to see if the GADInterstitial ad is ready, and if it is I attempt to present it. The problem is, when I dismiss the ad I notice that the ViewController that presented the ad is no longer there and instead it has sent me back to the ViewController that launched the ViewController that presented the ad.

Here's my code:

   - (void)viewDidLoad {
[super viewDidLoad];


BTRInterstitialHelper *helper = [BTRInterstitialHelper sharedHelper];
if([helper isInterstitialReady]) {
    [helper.interstitial presentFromRootViewController:self];
}

My helper method just looks like this:

    -(BOOL)isInterstitialReady {
if ([self.interstitial isReady]) {
    return YES;
} else {
    return NO;
}
 }

I do notice thought that when I present the ad, this message shows up in the logs:

  Presenting view controllers on detached view controllers is discouraged BTRIndividualLocationViewController: 0x7fd63bcd9c00.

Same problem occurs if I try to present it in viewWillAppear.

What am I doing wrong here?

EDIT: I have also tried the following, to try to pass in my app's actual rootViewController:

     UINavigationController *nav=(UINavigationController *)((BTRAppDelegate *)[[UIApplication sharedApplication] delegate]).window.rootViewController;
    UIViewController *rootController=(UIViewController *)[nav.viewControllers objectAtIndex:0];

    [helper.interstitial presentFromRootViewController:rootController];

Still the same result though, except that message doesn't pop up in the logs.


Solution

  • VC2.h

    #import "GADInterstitial.h"
    #import "GADInterstitialDelegate.h"
    

    set delegate

    @interface VC2 : UIViewController <GADInterstitialDelegate>
    

    and a property

    @property(nonatomic, strong) GADInterstitial *interstitial;
    

    VC2.m in viewdidappear

    self.interstitial = [[GADInterstitial alloc] init];
    self.interstitial.adUnitID = @"ca-app-pub-yourNumber";
    self.interstitial.delegate = self;
    
    GADRequest *request = [GADRequest request];
    

    for test ads in simulator

    request.testDevices = @[ GAD_SIMULATOR_ID ];
    

    coud also add location and keywords in the request (optional)

    then do request

    [self.interstitial loadRequest:request];
    

    listen to delegate

    -(void)interstitialDidReceiveAd:(GADInterstitial *)ad {
    
    if ([self.interstitial isReady]) {
    
    
        [self.interstitial presentFromRootViewController:self];
      }
    }
    

    If you want to show the interstitial only when pressed from a certain button use NSUserdefaults.

    In VC1 when button pressed

    [[NSUserDefaults standardUserDefaults] setValue:@"yes" forKey:@"showInterstitial"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    

    and wrap all the code for the interstitial in VC2 in viewdidappear in:

    if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"showInterstitial"] isEqualToString:@"yes"]) {
    
    }
    

    and add to -(void)interstitialDidReceiveAd:(GADInterstitial *)ad {

    [[NSUserDefaults standardUserDefaults] setValue:@"no" forKey:@"showInterstitial"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    

    p.s. you could also use a Bool for NSUserdefaults of course, I do use strings caus I had some errors with Bools, but Bools would be more correct I guess