1

Does anyone have decent code to run a function every n number of times a segue takes place? E.g. load an ad every time a user navigates 10 segues in my app.

At the moment I am doing something similar like so in my segue:

let chanceOfAd = Int(arc4random_uniform(UInt32(9)))

and then if the chanceOfAd == 0 , then I will load an ad. This is not ideal as it is a 1/10 chance of loading the ad, not every 10 segues. Of course this should average out the same amount of ad loads as if I did the every ten option, but some users will inevitably receive more ads than others, through the luck nature of the function at the moment.

So instead I want to load the ad every 10 times the segue takes place.

Chris Wright
  • 89
  • 2
  • 8

2 Answers2

0

Try declaring a global variable that is incremented whenever the function prepareForSegue is called.

prepareForSegue might look something like this

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    segueCounter++
    if (segueCounter % 10 == 0) {
        //you know that on this segue you should load an ad.
        //do something like telling the destination view controller to show the ad
    }

    //...

}
yesthisisjoe
  • 1,987
  • 2
  • 16
  • 32
0

Either subclass or extend the UIStoryBoardSegue class and custom the perfom : method using a global variable.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69