在启动一定次数后发出警报

发布于 2024-11-06 03:07:06 字数 138 浏览 4 评论 0原文

我正在研究这个想法,我希望在应用程序启动一定次数后(假设在 20 次启动后)弹出 UIAlert。

并且会有 2 个按钮。它将重置计数器,使警报在另外 20 次启动后出现。 还有一个按钮可以让它消失并且永远不会再出现。

我该怎么做呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自由范儿 2024-11-13 03:07:06

看一下 NSUserDefaults 来存储应用程序启动的次数。

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

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

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"];
}
话少情深 2024-11-13 03:07:06

在您的 AppDelegate applicationDidFinishLaunching:withOptions: 方法中检查 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"];
}

如果用户选择继续显示警报重写计数器,值为 0:

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

如果用户选择隐藏警报,则将计数器设置为 -1:

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

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"];
樱花坊 2024-11-13 03:07:06

设置一个柜台。每次应用启动时递增它并将其存储在 NSUserDefaults 中。每次检查是否小于20。如果等于20则重置并重新存储。
这有助于获取启动计数

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

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"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文