Objective-C:获取应用程序启动时的用户信息

发布于 2025-01-02 22:01:17 字数 222 浏览 2 评论 0原文

当我的应用程序启动时,我需要获取用户的性别。

如何仅在应用程序首次启动时调用该代码?我只想显示一个带有简单“你是男性还是女性?”的警报视图。然后存储选择以供应用程序的其余部分使用。我不希望用户每次打开应用程序时都被询问,只是在第一次打开它时被询问。

谢谢,

杰克

编辑

我知道如何实际存储数据,但是我在哪里调用代码以便仅在应用程序加载时显示警报视图?

I need to get the users sex when my application starts up.

How do I call the code only when the application first starts up? I just want to display an alert view with a simple "Are you Male or Female?" and then store the selection for use throughout the rest of the application. I don't want the user to be asked every time they open the app, just the first time they open it.

Thanks,

Jack

EDIT

I know how to actually store the data, but where do I call the code so the alert view is only displayed when the application loads up?

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

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

发布评论

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

评论(2

心清如水 2025-01-09 22:01:17

编辑

你可以放置它,以便在听起来

application:didFinishLaunchingWithOptions:

像是 NSUserDefaults 的一个很好的用途。这取决于您想要如何存储该信息,例如,如果我要使用字符串(我选择字符串是因为检测是否已设置值比使用 int 更容易)我可以有类似的东西然后

NSString * const PSUserSexKey = @"PSUserSexKey";

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

// If this returns nil then no value has been set yet
if (![userDefaults stringForKey:PSUserSexKey]) {
    //.. present dialog for picking sex
}

,当用户选择他们的性别时,您可以使用以下内容保存它:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

[userDefaults setObject:sex forKey:PSUserSexKey];
[userDefaults synchronize];

Edit

You could place this so that is get's called in

application:didFinishLaunchingWithOptions:

Sounds like a good use for NSUserDefaults. It depends how you want to store that information for example if I was to use a string (I've chosen string as detecting whether no value is already set is easier than using int) I could have something like this on start up

NSString * const PSUserSexKey = @"PSUserSexKey";

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

// If this returns nil then no value has been set yet
if (![userDefaults stringForKey:PSUserSexKey]) {
    //.. present dialog for picking sex
}

Then when a user has selected their sex you can save it with something like:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

[userDefaults setObject:sex forKey:PSUserSexKey];
[userDefaults synchronize];
花心好男孩 2025-01-09 22:01:17

无论您使用 Paul.s 还是 Regexident 的答案,将代码放在哪里并不重要。它仍然只会执行一次。

但是,如果您询问要将其放在哪里以便在应用程序启动后立即显示,则需要将其放在应用程序的第一个视图控制器的 viewDidLoad 中(可能在最后)呈现给用户。

Whether you use Paul.s or Regexident's answers, it doesn't matter too much where you put the code. It'll still only be executed once.

But if you're asking where you want to put it so that it shows up immediately after the app launches, you want to put it in viewDidLoad (probably at the end) for the first view controller your app presents to the user.

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