I am using the following code to save values from a settings view that takes values from a UITextField and stores them with NSUserDefaults. The code below even calls synchronize yet it is not saving the changes. What am I doing wrong here?
- (IBAction)save {
NSLog(@"save");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (self.usernameTextField.text != nil) {
NSLog(@"username: %@", self.usernameTextField.text);
[defaults setObject:kTwitterUsernameKey forKey:self.usernameTextField.text];
}
if (self.passwordTextField.text != nil) {
NSLog(@"password: %@", self.passwordTextField.text);
[defaults setObject:kTwitterPasswordKey forKey:self.passwordTextField.text];
}
[defaults synchronize];
[self dismissModalViewControllerAnimated:TRUE];
}
You are setting the key to the username and password, with the username/password string as the value.
So, you’ll never be able to retrieve this data (unless you know the username and password already 😉 )
Try swapping as shown above.