IOS Admob 插页式广告 for objective-c.



我已经在viewDidLoad上实现了插页式广告,但我需要帮助。我希望插页式广告不会在每次 viewDidLoad 出现时都显示,例如我希望它显示一次是,一次否。成为有案例的东西。

  • 案例 1 显示
  • 案例 2 不显示
  • 案例 3 显示
  • ......

这是我的代码:

 - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.interstitial = [[GADInterstitial alloc] initWithAdUnitID:@"a151b7d316a5c1d"];
    self.interstitial.delegate = self;
    [self.interstitial loadRequest: [GADRequest request]];
}
- (void)interstitialDidReceiveAd:(GADInterstitial *)interstitial {
    [interstitial presentFromRootViewController:self];
}

我对我的广告做同样的一次性,一次性。我只是使用用户默认值来持有计数器。也许尝试以下方法。下面的代码写得很清楚,所以它有点长,但是一旦你知道它在做什么,你可以进去收紧它(例如,使用adCount += 1;而不是adCount = adCount + 1;等。 希望这有帮助!

- (void)viewDidLoad
{
    [super viewDidLoad];
    //Call to your helper method
    //Stop viewDidLoad here if the method returns NO
    if (![self _shouldShowInterstitial]) return;
    self.interstitial = [[GADInterstitial alloc];
    initWithAdUnitID:@"a151b7d316a5c1d"];
    self.interstitial.delegate = self;
    [self.interstitial loadRequest: [GADRequest request]];
}
- (BOOL)_shouldShowInterstitial
{
    //1. Recover user default holding a counter
    BOOL retVal = NO;
    NSString *adKey = @"shouldShowInterstitialOnLoadDefaultKey";
    NSInteger adCount = [[NSUserDefaults standardUserDefaults]integerForKey:adKey];
    //2. If the counter is 1, return YES for showing an ad and reset the count to 0
    if (adCount > 0) {
        retVal = YES;
        adCount = 0;
    //3. If the count is 0, no ad is required, but increment it so next time the ad will show
    } else {
        retVal = NO;
        adCount = adCount + 1;
    }
    //4. Save your defaults
    [[NSUserDefaults standardUserDefaults] setInteger:adCount forKey:adKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
    //5. Return your bool value
    return retVal;
}