Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top