在某些iOS活动中暂停Admob非页内广告

时间:2015-02-22 19:03:55

标签: ios admob interstitial

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

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

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

1 个答案:

答案 0 :(得分:2)

设置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");
    }
}
相关问题