Search code examples
iosxcoderevmob

Show fullscreen ad once every two time a button is clicked


I'm using revmob to display ads in my app. I used to have it so that everytime the stop button for my timer was pressed, an ad would show. I want to make it so that it shows less. For example, the user press the button once, nothing happens. The second time, the ad shows up. So 1 out of 2 times, the ad shows up. 2 out of 4 times, the ad shows up, etc.

Code i'm using to launch the ad:

- (IBAction)Stop {
[timer invalidate];
if (self.fullscreen) [self.fullscreen showAd];
}

I just want a way to count how many time the stop button has been pressed and after every 2 times, show an ad =) Thanks in advance.


Solution

  • You just need to track the number of times it has displayed. I've tried to keep the code understandable, so it is a little overkill.

    @property (nonatomic, assign) int currentViews;
    @property (nonatomic, assign) int maxViewCount;
    

    Then in viewDidLoad (or equivalent)

    self.currentViews = 0;
    self.maxViewCount = 2; // show it every 2nd view
    

    Then increment and check:

    - (IBAction) Stop {
        [timer invalidate];
        self.currentViews++;
    
        if ((self.fullscreen) && (self.currentlViews == self.maxViewCount)) { 
             self.currentViews = 0; // reset the view count
             [self.fullscreen showAd];
        }