Reading and writing data in to plist r working fine.But when i run the app second time,I found that the datas r not saved.
Code:
-(void)readFromList
{
NSLog(@"readFromList");
NSString* plistPath = nil;
NSFileManager* manager = [NSFileManager defaultManager];
if (plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Jsonapp.plist"])
{
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:plistPath] retain];
for (id key in plistData ) {
user = [plistData objectForKey:@"Username"];
pass=[plistData objectForKey:@"Password"];
NSLog(@"user=%@",user);
NSLog(@"bundle: key=%@, value=%@", key, [plistData objectForKey:key]);
}
if(edit == TRUE)
{
txtPassword.text=user;
txtUsername.text=pass;
}
}
}
-(void)writeToList
{
NSLog(@"Write to plist");
NSString *error;
NSString* plistPath = nil;
NSFileManager* manager = [NSFileManager defaultManager];
if (plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Jsonapp.plist"])
{ NSLog(@"plistpath=%@",plistPath);
if ([manager isWritableFileAtPath:plistPath])
{
NSMutableDictionary* infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
[infoDict setValue:txtPassword.text forKey:@"Password"];
[infoDict setValue:txtUsername.text forKey:@"Username"];
[infoDict writeToFile:plistPath atomically:NO];
[manager changeFileAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] atPath: [[NSBundle mainBundle] bundlePath]];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:infoDict
format:NSPropertyListXMLFormat_v1_0
errorDescription:&error];
}
}