Xcode - 每出现 x 次视图就展示广告

Xcode - Show ad every x times view appears

我已经实施了 AdMob 框架并使其正常工作,因此它可以显示,但我需要每 3 次视图显示一次广告,我被卡住了。

编辑: @property (nonatomic, strong) GADInterstitial *interstitial;

我目前拥有的:

- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES];


static long int counter = 1;



NSLog(@"Before: %ld", counter);

if ([self.interstitial isReady] && !(counter++ % 3)) {
    [self.interstitial presentFromRootViewController:self];

    counter = 1;
    NSLog(@"After increase : %ld", counter);
}

}

问题是每次显示广告时,计数器都会增加 2 倍,这意味着广告显示奇数次。

也许尝试将 counter 声明为全局变量:

int counter;

然后执行以下操作:

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];

    if ([self.interstitial isReady] && counter >= 3) {
        [self.interstitial presentFromRootViewController:self];
        counter = 0;
    } else {
        counter++;
        NSLog(@"After increase : %d", counter);
    }
}