iPhone App not saving values to .plist file

Posted by Trent on Stack Overflow See other posts from Stack Overflow or by Trent
Published on 2009-12-18T19:04:10Z Indexed on 2010/04/10 5:23 UTC
Read the original article Hit count: 374

Filed under:
|

Hey everyone. I am working on an app that displays a modal the first time the consumer uses it. I have a .plist file that will store the information I request once the Save button is pressed. I can read from the .plist file fine and when I run my save method it SEEMS to work fine but the .plist file is not updating. I'm not so sure of the problem here. I show the modal like this.

 - (void) getConsumerInfo {
 NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"consumer.plist"];
 NSMutableDictionary *plistConsumerInfo = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

 NSString *hasAppeared = [plistConsumerInfo objectForKey:@"HasAppeared"];

 if(hasAppeared != kHasAppeared) {
  ConsumerInfoViewController *tmpConsumerInfoVC = [[ConsumerInfoViewController alloc]
               initWithNibName:@"ConsumerInfoView" bundle:nil];
  self.consumerInfoViewController = tmpConsumerInfoVC;
  [self presentModalViewController:consumerInfoViewController animated:YES];
  [tmpConsumerInfoVC release];
 }
}

This is called by the viewDidLoad method in the first view phone when the app is started. Within the consumerInfoViewController I have textfields that have data entered and when the Save button is pressed it calls this method.

    - (IBAction)saveConsumerInfo:(id)sender {
 NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"consumer.plist"];
 NSMutableDictionary *plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

 NSString *tmpDiversName = txtDiversName.text;
 NSString *tmpLicenseType = txtLicenseType.text;
 NSString *tmpLicenseNum = txtLicenseNumber.text;
 NSString *tmpHasAppeared = @"1";
 NSString *tmpNumJumps = @"3";

 [plistDict setValue:[[NSString alloc] initWithFormat:@"%@", tmpDiversName] forKey:@"ConsumerName"];
 [plistDict setValue:[[NSString alloc] initWithFormat:@"%@", tmpLicenseType] forKey:@"LicenseType"];
 [plistDict setValue:[[NSString alloc] initWithFormat:@"%@", tmpLicenseNum] forKey:@"LicenseNumb"];
 [plistDict setValue:[[NSString alloc] initWithFormat:@"%@", tmpNumJumps] forKey:@"NumJumps"];
 [plistDict setValue:[[NSString alloc] initWithFormat:@"%d", tmpHasAppeared] forKey:@"Show"];
 [plistDict writeToFile:filePath atomically: YES];
 NSLog([[NSString alloc] initWithContentsOfFile:filePath]);
 [self dismissModalViewControllerAnimated:YES];
}

This all runs fine with no hiccups but the file is never updated. I would like it to update so I can use this information throughout the app and update the flag so it will not show the view again once the data is entered. Please let me know if there is anymore information you need. Thanks in advance!

© Stack Overflow or respective owner

Related posts about plist

Related posts about iphone