First run blues - simple N'est-ce pas?

Discussion in 'iOS Programming' started by IDMah, Apr 8, 2012.

  1. IDMah, Apr 8, 2012
    Last edited: Apr 9, 2012

    IDMah macrumors 6502

    IDMah

    Joined:
    May 13, 2011
    #1
    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
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    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?
     
  3. IDMah, Apr 9, 2012
    Last edited: Apr 9, 2012

    IDMah thread starter macrumors 6502

    IDMah

    Joined:
    May 13, 2011
    #3
    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!!!!
     

Share This Page