在某些 iOS activity 上暂停 Admob Interstitial

Pause Admob Interstitial on certain iOS activity

我正在 iOS 应用的主屏幕上显示一个全屏插页式广告,延迟 3 秒后。

但有时我会将用户从主屏幕带到 屏幕 B。在主屏幕上加载的插页显示在本屏幕 B.

我不希望它发生。如何取消插页式广告在屏幕 B 上的显示?

设置 BOOL 以跟踪屏幕上出现插页式广告的时间。当您转换到 屏幕 B 时,调用 [self cancelAd] 并且如果屏幕上显示插页式广告,它将被关闭。

@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");
    }
}