1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Using NSUserDefaults

Discussion in 'iPhone/iPad Programming' started by matthew858, Apr 19, 2009.

  1. macrumors member

    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:

    - (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:
    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


    Staff Member

  3. macrumors member

    Thanks for that. I have established I need the following:
    [setObject:(id)username forKey:(NSString *)twitterUsername];
    Can I just put this on a new line after username has been declared?
  4. Moderator


    Staff Member

    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