开发者

Problem in NSUserDefaults

I am creating app for jobsite. It will display login page first. when we enter username and password webservice will return userid as response. with this user id we can get jobs.

I want to put jobs screen first and check if user id is null then open loginview. if it is not null then load jobs in tableview. I used Nsuserdefaults to save response when user first enter username password. Every thing is working fine. but userdefault key is showing null in console but my condition is loading output also.. this is strange,.

my code is

-(void)CheckCond{
    NSUserDefaults* storeData = [NSUserDefaults standardUserDefaults];

    NSLog(@"Value for userdefault %@",[storeData objectForKey:@"userId"]);
    NSString *responseValue = [storeData objectForKey:@"userId"];

    NSLog(@"first REsponse value :%@",responseValue);

    if ([responseValue isEqualToString:@""]) {


        [self LoginLogout];


    }
    else
{
    NSLog(@"self.userId 开发者_JAVA百科%@",self.userId);



        NSUserDefaults* storeData = [NSUserDefaults standardUserDefaults];
        [storeData setObject:self.userId forKey:@"userId"];

        NSLog(@"%@",[storeData objectForKey:@"userId"]);
        [self loadOutput];

    }
}

Console output for first time login is

2011-09-09 13:21:29.438 ITClassifiedUniversal[2718:207] first REsponse value :(null) 2011-09-09 13:21:29.438 ITClassifiedUniversal[2718:207] self.userId 77533 2011-09-09 13:21:29.438 ITClassifiedUniversal[2718:207] 77533

After quitting app and restarting console output is :

2011-09-09 13:21:42.044 ITClassifiedUniversal[2731:207] Value for userdefault (null) 2011-09-09 13:21:42.045 ITClassifiedUniversal[2731:207] first REsponse value :(null) 2011-09-09 13:21:42.045 ITClassifiedUniversal[2731:207] self.userId (null) 2011-09-09 13:21:42.045 ITClassifiedUniversal[2731:207] (null)

AS above output shows NSUserdefault for userid is null?? whts the problem ? This user id is coming from login view

// Create and set up job view controller.
                jobView = [[JobViewController alloc] initWithNibName:@"JobView" bundle:nil];
                jobView.userId = self.response;

                // Create a nav controller with job view as its root controller.
                UINavigationController *jobViewNavController = [[UINavigationController alloc] initWithRootViewController:jobView];

                // Present the job view nav controller.
                [self presentModalViewController:jobViewNavController animated:YES];

                [jobView release];
                [jobViewNavController release];


    at last add synchronize statement then its give proper value
    NSUserDefaults* storeData = [NSUserDefaults standardUserDefaults];
if ([storeData objectForKey:@"userId"]==nil)
{
            [storeData setObject:self.userId forKey:@"userId"];

    [storeData synchronize];
}


After setting your userId, you need to save the defaults:

[storeData synchronize];

So that they can be picked up next time.

Also, if you object is nil, then it won't return YES to isEqualToString. Your if statement should be

if (!responseValue || [responseValue isEqualToString:@""])


You can used below code. it may give what you need.

[[NSUserDefaults standardUserDefaults] setValue:userID forKey:@"userID"];         
[[NSUserDefaults standardUserDefaults] synchronize];


Look at the AppDelegate.m of this sample code from Apple and you will know what was wrong with your code.

You have not called registerDefaults with the corresponding objects and keys for your NSUserDefaults.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜