#pragma mark SA_OAuthTwitterControllerDelegate
- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username {
NSLog(@"Authenicated for %@", username);
[_btnTwitter setTitle:username forState:UIControlStateNormal];
}
I have this code for displaying username for twitter ,and it works well,but when i navigate to another page and came back the title username of the button disappears,i know we have to set a NSUserDefault value in viewwillapper for checking the username exists or not.But i didn’t know how to handle the nSUserDefault,if anyone have an idea to solve this issue please help me.
Thanks in advance.
EDIT:
- (void)viewWillAppear:(BOOL)animated{
//Sets up the NSUserDefaults
//Sets up the NSUserDefaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//Checks if there is a saved User Name
if([defaults objectForKey:@"kTwitterUserName"])
NSString *username = [defaults objectForKey:@"kTwitterUserName"];
[_btnTwitter setTitle:username forState:UIControlStateNormal];
else
{
}
this is how my viewwillappear lokks
and
- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username {
NSLog(@"Authenicated for %@", username);
[_btnTwitter setTitle:username forState:UIControlStateNormal];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:username forKey:@"kTwitterUserName"];
[defaults synchronize];
}
this is how my twitermethod looks like
NSUserDefaults are very simple to work with. They are kind of like a dictionary, you set an object and a key. So to do what your trying to do, you can use something like the following:
Then to set an object in your defaults, you set an object/key pair like this:
It is important to call this method at some point:
[defaults synchronize];. This explicitly saves the defaults. So you might as well do it immediately after you set the username key.Then to read the saved username:
Your oAuth Method should look like this:
So your
viewWillAppearwill look something like this:Let me know in a comment if you need any clarification.