我已经阅读了这里的问题,但无法找出我的错误。
我知道KeyChain更好,但仅仅出于我的目的,我想将它存储在NSUserDefaults中。
当我尝试保存密码并重新输入密码时,它无法正常工作并继续使用ELSE。我在这里错过了哪一行?
#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
答案 0 :(得分:2)
NSUserDefault
个键区分大小写
使用@"Password"
[[NSUserDefaults standardUserDefaults] setValue:textField.text forKey:@"Password"];
然后使用@"password"
NSString * _password = [[NSUserDefaults standardUserDefaults] stringForKey:@"password"];
除了该问题,您还没有使用_password
变量,因此请使用它:
if ([passwordField.text isEqualToString:_password) {...}
是的,你是对的,钥匙串是保存密码而不是NSUserDefault
编辑:
保存:
[[NSUserDefaults standardUserDefaults] setObject:textField.text forKey:@"Password"];
retireve:
NSString * _password = [[NSUserDefaults standardUserDefaults] objectForKey:@"Password"];
TESTED&工作强>
您必须将密码保存在ok完成处理程序
中-(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"];
}
}