Using NSUserDefaults

Discussion in 'iOS Programming' started by matthew858, Apr 19, 2009.

  1. macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #1
    I am making an application that has an account linked to it. The first time the app is launched, the user enters their username and password. These details are then stored in NSStrings. What I want to do is, on launch, check whether the username string is empty or contains a username (if the app has launched before). I have the following code:

    AppDelegate:
    Code:
    - (void)applicationDidFinishLaunching:(UIApplication *)application {
    	
    	NSUserDefaults *twitterUsername = [NSUserDefaults standardUserDefaults];
    	username = [twitterUsername stringForKey:@"twitter username"];
    	
    	if(username == nil) {
    		[self firstTimeWelcomeMessage];
    	}		
    	
        [window addSubview:tabBarController.view];
    	
    }
    

    After username and password entry:
    Code:
    username = usernameTextField.text;
    password = passwordTextField.text;
    

    How would I go about putting the variable "username" into the NSUserDefault Twitter Username and have it successfully recognise on start-up that there is something stored in Twitter Username?
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  3. thread starter macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #3
    Thanks for that. I have established I need the following:
    Code:
    [setObject:(id)username forKey:(NSString *)twitterUsername];
    
    Can I just put this on a new line after username has been declared?
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    I suggest you need to read up on basic Objective-C as that is nothing like a valid method call. You need to learn the basics of the language before you try and write an application.
     

Share This Page