I have read through the questions here but cannot find out where I am going wrong.
I know KeyChain is better but just for my purposes I want to store it in NSUserDefaults.
When I try to save the password and reenter it, it doesn't work and continues to the ELSE. What line am I missing here?
#import "PasswordViewController.h"
#import "ViewController.h"
@interface PasswordViewController ()
@end
@implementation PasswordViewController
\
-(void) viewDidAppear: (BOOL) animated {
[super viewDidAppear: animated];
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"AlreadyRan"] )
{
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Password"
message:@"Set Your Password"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Do Some action here
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Password";
textField.secureTextEntry = YES;
[[NSUserDefaults standardUserDefaults] setObject:textField.text forKey:@"Password"];
[[NSUserDefaults standardUserDefaults] synchronize];
}];
[self presentViewController:alert animated:YES completion:nil];
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"AlreadyRan"];
}
}
- (IBAction)enterPassword {
NSString * _password = [[NSUserDefaults standardUserDefaults] objectForKey:@"Password"];
NSLog(@"passField = %@ | _password = %@", passwordField.text, _password);
if ([passwordField.text isEqual:_password]) {
//Password is Correct
NSString * storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"PhotoView"];
[self presentViewController:vc animated:YES completion:nil];
}
else {
//Password is wrong
[self dismissViewControllerAnimated:YES completion: nil];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(nonnull NSDictionary *)info {
image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[ImageView setImage:image];
[self dismissViewControllerAnimated:YES completion:NULL];
[self dismissViewControllerAnimated:YES completion:NULL];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)enterPassword:(id)sender {
}
@end
NSUserDefault
keys are case sensitive
You save with @"Password"
[[NSUserDefaults standardUserDefaults] setValue:textField.text forKey:@"Password"];
Then you retrieve with @"password"
NSString * _password = [[NSUserDefaults standardUserDefaults] stringForKey:@"password"];
In addition to that issue, you never used _password
variable, so use it:
if ([passwordField.text isEqualToString:_password) {...}
And yes you are right , keychain is the appropriate place to save password not NSUserDefault
EDIT:
save:
[[NSUserDefaults standardUserDefaults] setObject:textField.text forKey:@"Password"];
Retrieve:
NSString * _password = [[NSUserDefaults standardUserDefaults] objectForKey:@"Password"];
TESTED & WORKING:
You must save the password in the ok completion handler
-(void) viewDidAppear: (BOOL) animated {
[super viewDidAppear: animated];
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"AlreadyRan3"] )
{
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Password"
message:@"Set Your Password"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
NSString *password = alert.textFields[0].text;
[[NSUserDefaults standardUserDefaults] setObject:password forKey:@"Password"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"NSUserDefaults %@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Password";
textField.secureTextEntry = YES;
}];
[self presentViewController:alert animated:YES completion:nil];
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"AlreadyRan"];
}
}