gpt4 book ai didi

ios - 密码不保存

转载 作者:行者123 更新时间:2023-12-01 20:19:48 28 4
gpt4 key购买 nike

我已经阅读了这里的问题,但无法找出我哪里出错了。

我知道 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

最佳答案

NSUserDefault键区分大小写
您使用 @"Password" 保存

[[NSUserDefaults standardUserDefaults] setValue:textField.text forKey:@"Password"];
然后你用 @"password" 检索
NSString * _password = [[NSUserDefaults standardUserDefaults] stringForKey:@"password"];
除了那个问题,您从未使用过 _password变量,所以使用它:
if ([passwordField.text isEqualToString:_password) {...}
是的,你是对的,钥匙串(keychain)是保存密码的合适位置,而不是 NSUserDefault
编辑:
节省:
[[NSUserDefaults standardUserDefaults] setObject:textField.text forKey:@"Password"];
取回:
NSString * _password = [[NSUserDefaults standardUserDefaults] objectForKey:@"Password"];

测试和工作:
您必须将密码保存在 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"];
}
}

关于ios - 密码不保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35894238/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com