Reputation: 11
I have implemented interstitial ad on viewDidLoad, but I need help with this. I want Interstitial not to show every time the viewDidLoad appears, example I want it to show one time Yes, One time No. To be something with cases.
Here is my code:
- (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];
}
Upvotes: 0
Views: 653
Reputation: 1682
I do the same kind of one-on, one-off with my ads. I just use a user default to hold a counter. Maybe try the following. The code below is written to be clear so it's a bit long, but you could go in and tighten it up once you know what it's doing (eg. use adCount += 1;
instead of adCount = adCount + 1;
etc. Hope this helps!
- (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;
}
Upvotes: 1