Search code examples
iosadmobinterstitial

Pause Admob Interstitial on certain iOS activity


I am displaying a full screen interstitial ad, after a delay of 3 secs, on the home screen of my iOS app.

However sometimes I take the user to screen B from the home screen. The interstitial which was loaded on the home screen is displayed on this screen B.

I don't want it to happen. How can I cancel the interstitial from being shown on screen B?


Solution

  • Setup a BOOL to keep track of when an interstitial is on screen. When you transition to screen B, call [self cancelAd] and if there is an interstitial displaying on the screen it will be dismissed.

    @import GoogleMobileAds;
    
    #define INTERSTITIAL_UNIT_ID @"yourAdMobIDGoesHere"
    
    @interface ViewController () <GADInterstitialDelegate>
    
    @end
    
    @implementation ViewController {
        GADInterstitial *interstitial_;
        BOOL interstitialOnScreen;
    }
    
    -(void)viewDidLoad {
        [super viewDidLoad];
        // Setup BOOL
        interstitialOnScreen = NO;
    
        // Load AdMob ad
        interstitial_ = [[GADInterstitial alloc] init];
        interstitial_.adUnitID = INTERSTITIAL_UNIT_ID;
        interstitial_.delegate = self;
        [interstitial_ loadRequest:[GADRequest request]];
    
        // Present ad after 1 second
        [NSTimer scheduledTimerWithTimeInterval: 1.0
                                         target: self
                                       selector:@selector(showAdMobInterstitial)
                                       userInfo: nil repeats:NO];
    
        // Cancel ad after 4 seconds
        [NSTimer scheduledTimerWithTimeInterval: 4.0
                                         target: self
                                       selector:@selector(cancelAd)
                                       userInfo: nil repeats:NO];
    }
    
    -(void)showAdMobInterstitial {
        // Call when you want to show the ad
        // isReady will check if an ad has been loaded
        // Set interstitialOnScreen to YES
        if (interstitial_.isReady) {
            [interstitial_ presentFromRootViewController:self];
            interstitialOnScreen = YES;
            NSLog(@"presentFromRootViewController");
        }
    }
    
    -(void)interstitial:(GADInterstitial *)interstitial didFailToReceiveAdWithError:(GADRequestError *)error {
        NSLog(@"didFailToReceiveAdWithError");
        NSLog(@"%@", error);
    }
    
    -(void)interstitialWillDismissScreen:(GADInterstitial *)interstitial {
        NSLog(@"interstitialWillDismissScreen");
    }
    
    -(void)interstitialDidDismissScreen:(GADInterstitial *)interstitial {
        // User closed the ad so lets load a new one for the next time we want to present
        // Set interstitialOnScreen to NO
        interstitial_ = nil;
        interstitial_ = [[GADInterstitial alloc] init];
        interstitial_.adUnitID = INTERSTITIAL_UNIT_ID;
        interstitial_.delegate = self;
        [interstitial_ loadRequest:[GADRequest request]];
        interstitialOnScreen = NO;
        NSLog(@"interstitialDidDismissScreen");
    }
    
    -(void)cancelAd {
        if (interstitialOnScreen) {
            // Ad is currently on screen
            // Lets get rid of that ad
            [self dismissViewControllerAnimated:YES completion:nil];
            interstitialOnScreen = NO;
            NSLog(@"cancelAd");
        }
    }