NSUserDefaults,仅在第一次加载时执行某些操作

发布于 2024-12-13 17:02:52 字数 418 浏览 3 评论 0原文

我试图在应用程序第一次启动时显示警报。如果他们稍后进入设置页面并希望再次看到它,我还想显示此警报。我想我可以在 NSUserDefaults 中设置一个布尔值。我不太确定如何做到这一点(假设这种方法是正确的)。我认为在 applicationDidFinishLaunching:options 中,我可以获取键 @"FirstTimeLaunched" 的值,在 applicationWillTerminate 中,我可以设置该对象该键为NO。如果是这种情况,如何让 BOOL 第一次变为 YES

另外,我是否也需要在 didEnterBackground 模式下将其设置为 NO ?谢谢。

I'm trying to do show an alert the first time the app is launched. I'd also like to show this alert if they go into a settings page later on and want to see it again. I figured I could set a boolean in the NSUserDefaults. I'm not quite sure how to do that though (assuming this approach is right). I thought in applicationDidFinishLaunching:options, I could get the value for key @"FirstTimeLaunched", and in applicationWillTerminate, I could set the object for that key to NO. If this is the case, how do I get the BOOL to be YES for the first time?

Also, do I need to set it to NO in didEnterBackground mode as well? Thanks.

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

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

发布评论

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

评论(3

妖妓 2024-12-20 17:02:52

倒过来想想,不要期待 YES,而是在应用程序实际启动时设置一个变量,并在用户在设置中这么说时将其删除。

启动时:

if (![[NSUserDefaults standardUserDefaults] boolForKey:@"hasBeenLaunched"]) {
    // show your only-one-time view
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasBeenLaunched"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

当用户想再次看到它时,只需删除该密钥即可:

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"hasBeenLaunched"];

当访问不存在的密钥时,NSUserDefaults将返回nil

Just think it backwards, instead of expecting a YES, set a variable when the app is actually launched, and remove it when the user says so in the settings.

At launch:

if (![[NSUserDefaults standardUserDefaults] boolForKey:@"hasBeenLaunched"]) {
    // show your only-one-time view
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasBeenLaunched"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

And when the user wants to see it again, just remove the key:

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"hasBeenLaunched"];

When accessing a key that does not exists, NSUserDefaults will return nil.

烟酒忠诚 2024-12-20 17:02:52

我会采取相反的方法,首先启动检查名为 appHasLaunchedPreviously 的密钥。

如果是,则绕过警报。
如果不是“是”,则显示警报并立即为下次做好准备。

I would take the opposite approach and first launch check for a key called appHasLaunchedPreviously.

If it's yes then bypass the alert.
If it's not YES then show the alert and immediately set it ready for next time.

信愁 2024-12-20 17:02:52

第一次启动应用程序时,因此我认为您不需要将 BOOL 设置为 NO。如果应用程序被删除,UserDefaults 也会被删除。我们在App首次启动时设置它 didFinishLaunchingWithOptions:

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"FirstTimeLaunched"];
[[NSUserDefaults standardUserDefaults] synchronize];

如果您在applicationWillTerminate中将Bool设置为NO,那么每次用户从托盘中杀死应用程序时,您都会在应用程序启动时显示警报。同样,这取决于您的业务需求。

First time App is launched, so I don't think you will ever need to set that BOOL to NO. UserDefaults gets deleted if App is removed. and we set it when App is first launched application didFinishLaunchingWithOptions:

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"FirstTimeLaunched"];
[[NSUserDefaults standardUserDefaults] synchronize];

If you set Bool to NO in applicationWillTerminate, then everytime user kills app from tray you will show alert on app launched. Again it depends on your business requirements.

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