I am using below code for for saving the username password, credentials saved properly but the issue is when I again run the application, it shows the credentials in textfields but checkbox remains empty (i am using images to show the box checked and unchecked).
My questions are:
1. How can i save the state of checkbox if it is checked or unchecked and
2. If it is unchecked how can i remove the credentials from nsuserdefaults.
- (IBAction)checkboxButton:(id)sender{
if (checkboxSelected == 0){
[checkboxButton setSelected:YES];
NSString *user = [userNameField text];
NSString *passwd = [passwordField text];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:user forKey:@"Username"];
[defaults setObject:passwd forKey:@"Password"];
[defaults synchronize];
NSLog(@"Data Saved");
checkboxSelected = 1;
} else {
[checkboxButton setSelected:NO];
checkboxSelected = 0;
//Saving Username Password in file
}
}
thank you … 🙂
Well you can save the state of the checkbox in the
NSUserDefaults.To read do the following.
It will return
NO(false) if it is not set.To set the state:
To remove the values from the
NSUserDefaultsjust set the properties tonil:Also I can strongly advice you to store the password in the keychain.
since all the data saved in
NSUserDefaultsis stored plain text.You can use the easy to
SSKeyChainfor accessing keychain api more easily.