NSUserDefaults doubts in iphone SDK
<p>I am integrating twitter in my app for sharing a text. My architecture of twitter integration is I have two Button <code>_btnTwitter</code> and <code>_btntwitteLogout</code> and when the user successfully login the twitter his name will display in <code>_btntwitterLogout</code> as title.</p>

<p>When the user tap the <code>_btntwitterLogout</code> it logs out the twitter and logout button hides and login button comes.every thing went ok.but when the user tap the login button(<code>_btnTwitter</code>) the Twitter login popup cmes for login purpose,here is the problem that am facing ,when the popup comes the user tap the cancel button of that popup the popup disappears and here the previous username of the user in the logout button.it didn't changes. I have put user defaults to check.</p>

<pre><code>-(IBAction)_clickbtnTwitter:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"Twitter_logged"];
       [_btntwitterLogeout setTitle:nil forState:UIControlStateNormal];
        _btntwitterLogeout.hidden = NO;
        _btnTwitter.hidden=YES;
    if (_engine) return;
    _engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate: self];
    _engine.consumerKey = kOAuthConsumerKey;
    _engine.consumerSecret = kOAuthConsumerSecret;

    UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _engine delegate: self];

    if (controller) 
        [self presentModalViewController: controller animated: YES];
    else {
        [_engine sendUpdate: [NSString stringWithFormat: @"Already Updated. %@", [NSDate date]]];

    }
}
}
</code></pre>

<p>then logout code</p>

<pre><code>-(IBAction)_clickbtntwitterlogeout:(id)sender
{ 

    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"Twitter_logged"];
    crosstwitterimage.hidden = YES;
     [_btntwitterLogeout setTitle:nil forState:UIControlStateNormal];
   _btntwitterLogeout.hidden = YES;
    _btnTwitter.hidden=NO;
    _btnTwittermain.enabled = NO;
    [_engine clearAccessToken];
    [_engine clearsCookies];
    [_engine setClearsCookies:YES];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"authData"];
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"authName"];
    NSLog(@"%@",[[NSUserDefaults standardUserDefaults]valueForKey:@"authName"]);
    NSLog(@"%@",[[NSUserDefaults standardUserDefaults]valueForKey:@"authData"]);
   [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"Twitter_logged"];
    [_engine release];
    _engine=nil;   

    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies])
    {
        NSString* domainName = [cookie domain];
        NSRange domainRange = [domainName rangeOfString:@"twitter"];
        if(domainRange.length &gt; 0)
        {
            [storage deleteCookie:cookie];

        }
    }

}
</code></pre>

<p>in viewwillappear</p>

<pre><code>BOOL logged = [[NSUserDefaults standardUserDefaults] boolForKey:@"Twitter_logged"];
;
if (logged == YES) {
    _btnTwitter. hidden = YES;
    _btntwitterLogeout.hidden = NO;
    crosstwitterimage.hidden = NO;
    _btnTwittermain.enabled =YES;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    //Checks if there is a saved User Name    
    if([defaults objectForKey:@"kTwitterUserName"])
    {
        NSString *username = [defaults objectForKey:@"kTwitterUserName"];
        [_btntwitterLogeout setTitle:username forState:UIControlStateNormal];
        crosstwitterimage.hidden = NO;
    }
}
    else
    {
        _btnTwitter. hidden = NO;
        crosstwitterimage.hidden = YES;
        [_btntwitterLogeout setTitle:nil forState:UIControlStateNormal];
        _btnTwittermain.enabled =NO;
        crosstwitterimage.hidden = YES;

    }
</code></pre>

<p>but when i tap the cancel button in loginwindow of twitter,an comes back the username with the logout button shows ,if the user is already logedout from twitter.
is there any problem in my code.
thanks in advance.</p>

<p>EDIT</p>

<pre><code>#pragma mark SA_OAuthTwitterEngineDelegate
- (void) storeCachedTwitterOAuthData: (NSString *) data forUsername: (NSString *) username {
    NSUserDefaults          *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject: data forKey: @"authData"];
    [defaults synchronize];
}

- (NSString *) cachedTwitterOAuthDataForUsername: (NSString *) username {
    return [[NSUserDefaults standardUserDefaults] objectForKey: @"authData"];
}

    #pragma mark SA_OAuthTwitterControllerDelegate
    - (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username {
        NSLog(@"Authenicated for %@", username);
        [_btntwitterLogeout setTitle:username forState:UIControlStateNormal];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:username forKey:@"kTwitterUserName"];
        [defaults synchronize];
        }

    - (void) OAuthTwitterControllerFailed: (SA_OAuthTwitterController *) controller {
        NSLog(@"Authentication Failed!");
    }

    - (void) OAuthTwitterControllerCanceled: (SA_OAuthTwitterController *) controller {
        NSLog(@"Authentication Canceled.");
    }


    #pragma mark TwitterEngineDelegate
    - (void) requestSucceeded: (NSString *) requestIdentifier {
        NSLog(@"Request %@ succeeded", requestIdentifier);
    }

    - (void) requestFailed: (NSString *) requestIdentifier withError: (NSError *) error {
        NSLog(@"Request %@ failed with error: %@", requestIdentifier, error);
    }
</code></pre>
