在NSUserDefaults中存储多个键值对

时间:2016-07-07 08:00:12

标签: ios objective-c

我正在IOS(xcode 6.4)中构建一个简单的登录和注册。要保存用户名和密码,我正在使用NSUserDefaults。但问题是我无法存储多个用户名和密码,因为我将数据保存到同一个密钥。实现它的替代方法是什么。我在stackoverflow上看到了一些类似的问题,但无法理解解决方案。所以任何人都可以轻松解释。 HEre是我的代码:

//function for login pressed
- (IBAction)login:(UIButton *)sender 
{
    NSLog(@"%@,%@",userNameField.text,passwordField.text);

    username = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
    password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
    NSLog(@"%@,%@",username,password);
    bool validUser = [username isEqualToString:userNameField.text];
    bool validPassword = [password isEqualToString:passwordField.text];

    if ([self checkWhetherFieldIsEmpty:userNameField.text :passwordField.text]) 
    {
        [self displayAlert:@"All Fileds are mandatory"];
    }
    else if(validUser && validPassword)
    {
        [self displayAlert:@"Successfull Login"] ;
    }
    else{
        // NSLog(@"Invalid username or password");
        [self displayAlert:@"Invalid login or password"];
    }

}

//function when register button is pressed
- (IBAction)register:(UIButton *)sender 
{
    if([[[NSUserDefaults standardUserDefaults] objectForKey:@"username"] isEqualToString:userNameField.text])
    {
        [self displayAlert:@"User already exist"];
        userNameField.text = @"";
        passwordField.text = @"";
    }
    else 
    {
        [[NSUserDefaults standardUserDefaults] setObject:userNameField.text forKey:@"username"];
        [[NSUserDefaults standardUserDefaults] setObject:passwordField.text forKey:@"password"];
        [[NSUserDefaults standardUserDefaults]synchronize];
        NSLog(@"user Created successfully");
        [self displayAlert:@"Registration Successfull"];
    }
}

2 个答案:

答案 0 :(得分:1)

正如Simon指出的,在NSUserDefaults中存储用户名和密码存在安全问题。但是,如果你仍想这样做,那就很简单了。

NSUserDefaults实际上不仅仅是plist,因此您可以在NSArray中存储从NSDictionaryNSUserDefaults创建的任意复杂结构。所以你可以这样做:

NSArray<NSDictionary*>* accounts = @[@{
    @"username" : @"firstUsername",
    @"password" : @"firstPassword"
}, @{
    @"username" : @"secondUsername",
    "password" : @"secondPassword"
}];
[[NSUserDefaults standardUserDefaults] setObject: accounts forKey:@"accounts"];

请注意,您无法仅更改一个帐户;如果您需要更改或添加新帐户,您应该阅读整个结构,将数组和字典转换为可变变体,并将它们重新写入默认值。

<强>更新

NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray* mutableAccounts = [[defaults objectForKey:@"accounts"] mutableCopy];
// Add or remove accounts:
[mutableAccounts addObject:@{ @"username" : @"newAccount", @"password" : @"newPassword" }];
[mutableAccounts removeObjectAtIndex:0];
[defaults setObject:mutableAccounts forKey:@"accounts"];

答案 1 :(得分:0)

您可以简单地将密码保存为密钥用户名的对象,有些人认为:

- (IBAction)login:(UIButton *)sender {

    NSLog(@"%@,%@",userNameField.text,passwordField.text);

    NSString *password = userNameField.text.length > 0? [[NSUserDefaults standardUserDefaults] objectForKey:userNameField.text] : nil;
    bool validUser = passwordField.text.length > 0 ? [passwordField.text isEqualToString:password] : NO;

    if ([self checkWhetherFieldIsEmpty:userNameField.text :passwordField.text]) {
        [self displayAlert:@"All Fileds are mandatory"];
    }

    else if(validUser){

        [self displayAlert:@"Successfull Login"] ;


    }

    else{
        // NSLog(@"Invalid username or password");
        [self displayAlert:@"Invalid login or password"];
    }

}

//function when register button is pressed
- (IBAction)register:(UIButton *)sender {
    //TODO: need check userNameField passwordField is not nil here
    if([[[NSUserDefaults standardUserDefaults] objectForKey:userNameField.text] length] > 0){
        [self displayAlert:@"User already exist"];
        userNameField.text = @"";
        passwordField.text = @"";

    }
    else {
        [[NSUserDefaults standardUserDefaults] setObject:passwordField.text forKey:userNameField.text];
        [[NSUserDefaults standardUserDefaults]synchronize];
        NSLog(@"user Created successfully");
        [self displayAlert:@"Registration Successfull"];
    }
}