如果正确,则在Plist中进行凭据编辑

时间:2012-12-14 07:54:37

标签: iphone ios plist

在plist中,我存储了凭据,我需要检查正确的凭据,如果正确的凭据匹配,则将密码字段替换为new ..for this I Have 3 UITextField。 A表示新的电子邮件ID,b表示当前密码,c表示新密码..如果a==b表示current password == email id,则在c文本字段中输入的新密码应替换为{{1}中的当前密码字段} eneter here

plist

2 个答案:

答案 0 :(得分:1)

检查这是否适合您。

- (void)authenticateCredentials {

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                 NSUserDomainMask, YES);
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"XYZ.plist"];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:documentPlistPath];
    NSArray *plistArray = [dict objectForKey:@"title"];

    NSMutableArray *plistUpdatedArray = [NSMutableArray arrayWithArray:plistArray];

    for (int i = 0; i< [plistArray count]; i++)
    {
        id object = [plistArray objectAtIndex:i];

        if ([object isKindOfClass:[NSDictionary class]]) {
            NSDictionary *objDict = (NSDictionary *)object;

            if ([[objDict objectForKey:@"pass"] isEqualToString:emailTextFeild.text] && [[objDict objectForKey:@"title"] isEqualToString:passwordTextFeild.text])
            {
                NSLog(@"Correct credentials");

                NSMutableDictionary *dict1 = [NSMutableDictionary dictionaryWithDictionary:objDict];
                [dict1 setObject:newPassword forKey:@"title"];

                [plistUpdatedArray replaceObjectAtIndex:i withObject:dict1];

                NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithDictionary:dict];
                [dict2 setObject:plistUpdatedArray forKey:@"title"];
                [dict2 writeToFile:documentPlistPath atomically:YES];

                return;
            }
            NSLog(@"INCorrect credentials");
        } else {
             NSLog(@"Error! Not a dictionary");
        }
    }
}

答案 1 :(得分:1)

如果您的plist是捆绑包,则无法更改其中存储的值。 因此,在进行任何更改之前,请将其复制到文档目录。

回答你的问题:

if([emailField.text isEqualToString:currntPwd.txt])
{
   NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
  [newDict addEntriesFromDictionary:objDict];
  [newDict setObject:@"Midhun" forKey:@"pass"];
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                             NSUserDomainMask, YES);
   NSString *docDirectory = [paths objectAtIndex:0];
   NSString *plistPath = [docDirectory stringByAppendingPathComponent:@"yourPlist.plist"];
   [[NSFileManager defaultManager] removeItemAtPath:plistPath];
   [newDict writeToFile:plistPath atomically:YES];
}