Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

IDMah

macrumors 6502
Original poster
May 13, 2011
317
11
Thinking this should be easy but.

I want the user to input a username and password on the first run.
then I save the them via.

Code:
(void)demandUserSetup{
// some code to display and get textfields //
// and wait for ok to be pushed //

[[NSUserDefaults standardUserDefaults] setObject:myLoginTextField.text forKey:@"myLogIn"];
[[NSUserDefaults standardUserDefaults] setObject:myPasswordTextField.text forKey:@"myPassWrd"];
}

but of course If I check before they are written then BadStuff happens:

Code:
NSString *userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"myLogIn"]; //<--- crashy part I think

if (!userName){
     [self demandUserSetup]
}

is there a way to pre-install a standardUserDefaults forkey:mad:"myLogin" so it won't crash???

Or any other way to figure out if it's the first run and that the Login and password have been set correctly..

thanks
Ian
 
Last edited:
Why are you setting an object for key on your "crashy" line?

Maybe I misunderstand how these code snippets are supposed to fit together, but shouldn't that line be accessing, not setting, an object in NSUserDefaults?
 
typo

Yes replaced above. Stupid tired typo..

Doh!!! ok it wasn't that line that was crashing..

for Googlers..

Just. Something like.

Code:
-(void)viewDidLoad
{
   [super viewDidLoad];
   NSLog(@"viewDidLoad\n");
   NSString *loginName = [[NSUserDefaults standardUserDefaults]objectForKey:@"myLogIn"];
   NSLog(@"login Name :%@",loginName);
	if (!loginName) {
		[self demandUserSetup];
	}
}

So I guess it was easy.. for smarter people than I.. hahaaa!!!!
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.