Search code examples
iphoneobjective-cuialertviewalert

Making an Alert apper after a certain amount of lanuches


I am working on this idea where I want an UIAlert to pop up after a certain amount of launches of the app (let's say after 20 launches).

And there's going to be 2 buttons. One that will reset the counter which will make the alert appear after another 20 launches. And one button that will make it disappear and never appear again.

How I would do this?


Solution

  • In your AppDelegate applicationDidFinishLaunching:withOptions: method check NSUserDefaults:

    int counter = [[NSUserDefaults standardUserDefaults] integerForKey:@"LaunchesCounter"];
    if (counter == -1)
    { /* Cancel chekcing, cause earlier user choose hide alert */ }
    else if (counter >= 20)
    { /* Show alert */ }
    else // Increment counter
    {
        ++counter;
        [[NSUserDefaults standardUserDefaults] setInteger:counter forKey:@"LaunchesCounter"];
    }
    

    If user choose continue to show alert rewrite counter with 0:

    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"LaunchesCounter"];
    

    If user choose to hide alerts set counter to -1:

    [[NSUserDefaults standardUserDefaults] setInteger:-1 forKey:@"LaunchesCounter"];