title: Reading and Writing to plist (iPhone Programming)
body: <p>I am trying to implement a simple plist example from "Beginning iPhone 3 Development book". I looked into the code but my data was never saved to a plist file. Actually my project site map is as follows: Whenever you launch the app it fires in TestViewController. On the TestViewController, there is a button. When you click on the button it pushes another view controller which is PersistenceViewController and here is the code I wrote in PersistenceViewController. My doubt: is the applicationWillTerminate being called in this method? I don't think so..please help. I am learning how to persist the data now.</p>
  
  <pre><code>In .h file #define kFilename @"data2.plist"
  
   - (NSString *)dataFilePath {
          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
          NSString *documentsDirectory = [paths objectAtIndex:0];
          NSString *path = [documentsDirectory stringByAppendingPathComponent:kFilename];
          return path;
      }
  
      - (void)applicationWillTerminate:(NSNotification *)notification {
          NSMutableArray *contactFormArray = [[NSMutableArray alloc] init];
          NSLog(@"App Terminate:%d",[contactFormArray count]);
          [contactFormArray addObject:nameField.text];
          [contactFormArray addObject:emailField.text];
          [contactFormArray addObject:phoneField.text];
          [contactFormArray addObject:companyField.text];
          [contactFormArray writeToFile:[self dataFilePath] atomically:YES];
          [contactFormArray release];
      }
  
  
      // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
      - (void)viewDidLoad {
  
          NSString *filePath = [self dataFilePath];
          if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
              NSArray *contactFormArray = [[NSArray alloc] initWithContentsOfFile:filePath];
              NSLog(@"Did Load:%d",[contactFormArray count]);
              nameField.text = [contactFormArray objectAtIndex:0];
              emailField.text = [contactFormArray objectAtIndex:1];
              phoneField.text = [contactFormArray objectAtIndex:2];
              companyField.text = [contactFormArray objectAtIndex:3];
              [contactFormArray release];
          }
  
          UIApplication *app = [UIApplication sharedApplication];
          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:)name:UIApplicationWillTerminateNotification object:app];
          [super viewDidLoad];
  
      }
  </code></pre>
  
  <p>Thanks for any valuable suggestions...</p>