KingPolygon
KingPolygon

Reputation: 4755

Checking if Username Exists in NSUserDefaults?

I currently want to check if a username exists in NSUserDefaults, if it does not I want to load a modal view controller. The problem is that even though the username is nil, it's still acting as if it exists. Here's what I have:

//Check if the login data exists
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"username"] != nil) {

        //Load Login View if no username is found
        NSLog(@"No username found");
        LoginViewController *loginView = [[LoginViewController alloc] init];
        [self presentViewController:loginView animated:YES completion:nil];
    }

    else {

        NSString *savedUsername = [defaults stringForKey:@"username"];
        NSLog(@"Username found: %@", savedUsername);

    }

I keep getting this in my NSLog: Username found: (null)

Any help would be appreciated!

Upvotes: 0

Views: 2916

Answers (8)

Thilina Chamath Hewagama
Thilina Chamath Hewagama

Reputation: 9040

First of all, delete the app from your phone/simulator

enter image description here

Replace the code with this, and run

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"username"] == nil) {

        //Load Login View if no username is found
        NSLog(@"No username found");

        //set the username,
       [defaults setObject:@"username_1" forKey:@"username"];

        LoginViewController *loginView = [[LoginViewController alloc] init];
        [self presentViewController:loginView animated:YES completion:nil];
    }

    else {

        NSString *savedUsername = [defaults stringForKey:@"username"];
        NSLog(@"Username found: %@", savedUsername);

    }



Good To Know:
Once you save an object for key "username", later this condition always returns false,

([defaults objectForKey:@"username"] == nil)

If you delete the previous installation. The UserDefaults automatically gets deleted. then only you can try whether this is working ([defaults objectForKey:@"username"] == nil)

Upvotes: 2

Jitendra
Jitendra

Reputation: 5081

try this way.

NSUserDefaults *Def = [NSUserDefaults standardUserDefaults];

    if (![Def objectForKey:@"Username"]) 
    {
       // Code 
    } 
    else
    {
       //Code
    }

this will really help you.

Upvotes: 0

Buntylm
Buntylm

Reputation: 7373

You can simply try this no need to use nil.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if (![defaults objectForKey:@"Username"]) {

} else {

}

Upvotes: 2

Martin R
Martin R

Reputation: 539745

As others already pointed out, your test

if ([defaults objectForKey:@"username"] != nil)

is wrong. But even with

if ([defaults objectForKey:@"username"] == nil)

you have a problem if the user defaults contain a value for "username" that is not a string. In that case objectforKey returns a value, but stringForKey returns nil.

Therefore, you should use only stringForKey:

NSString *savedUsername = [defaults stringForKey:@"username"];
if (savedUsername == nil) {
    //Load Login View if no username is found
    NSLog(@"No username found");
    // ...
} else {
    NSLog(@"Username found: %@", savedUsername);
}

Upvotes: 1

ios_av
ios_av

Reputation: 324

If you are confused in NULL and NIL ,You can also use this approach:-

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

NSString*value=[prefs objectForKey:@"username"];

NSLog(@"useremail %@",value);

value=[value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

if ([value length]>0)
{


      NSString *savedUsername = [defaults stringForKey:@"username"];
    NSLog(@"Username found: %@", savedUsername);

}

else {

    NSLog(@"No username found");
    LoginViewController *loginView = [[LoginViewController alloc] init];
    [self presentViewController:loginView animated:YES completion:nil]; 
  }

Upvotes: 0

Mike Weller
Mike Weller

Reputation: 45598

You are using != nil which is the opposite of what you want. Change your first if statement to:

if ([defaults objectForKey:@"username"] == nil) {

Or just:

if (![defaults objectForKey:@"username"]) {

Upvotes: 2

Unnati
Unnati

Reputation: 2457

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"username"] == nil) {

        //Load Login View if no username is found
        NSLog(@"No username found");
        LoginViewController *loginView = [[LoginViewController alloc] init];
        [self presentViewController:loginView animated:YES completion:nil];
    }

    else {

        NSString *savedUsername = [defaults stringForKey:@"username"];
        NSLog(@"Username found: %@", savedUsername);

    }

Upvotes: 10

adali
adali

Reputation: 5977

if ( ! [defaults objectForKey:@"username"] )

Upvotes: 3

Related Questions