开发者

Making an Alert apper after a certain amount of lanuches

开发者 https://www.devze.com 2023-03-03 02:38 出处:网络
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).

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开发者_如何学Python 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?


Take a look at NSUserDefaults to store a count of the number of times the app has started.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
uint count = [defaults integerForKey:@"num_launches"];

if (count > 20) {
    // Show alert
} else {
   count ++;
   [defaults setInteger:count forKey:@"num_launches"];
}


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"];


Set up a counter. Increment it each time the app launches and store it in NSUserDefaults. Check it each time to make sure it is less than 20. If it is equal to 20 then reset and store again. This helps to get launch count

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger launchCount = [prefs integerForKey:@"launchCount"];
if (launchCount > limit) {
     // Show alert
} else {
 launchCount ++;
 [prefs setInteger:count forKey:@"launchCount"];
}

launchCount++;
NSLog(@"Application has been launched %d times", launchCount);
[prefs setInteger:launchCount  forKey:@"launchCount"];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号