Data Persistence In iPhone


In this application we enter our data to the textfield and save those data to the property list when application quits and reload data again from property list when application launched. So let see how it will be worked.

Step 1: Create a application using View base application template. Give the application name “DataPersistence”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Xpand classes and notice Interface Builder created the DataPersistenceViewController class for you. Expand Resources and notice the template generated a separate nib, DataPersistenceViewController.xib, for the “DataPersistence”.

Step 4: We need to add one property list in the Resource folder.

Step 5: Open the DataPersistenceViewController.h file and we have added two TextField  Outlet ,defined a constant for filename  and  add two methods.So make the following changes in the file.

 

 

 

Step 6: Double click the DataPersistenceViewController.xib file and open it to the Interface Builder. First drag two textField from the library and place it to the View window and drag two label from the library and place it to the view window, change the  label names into Your FirstName and Your LastName(See the figure below). Connect the File’s Owner icon to the View icon select view, connect File’s Owner icon to the first TextField and select field1. Do it same for next TextField and select field2. Now save the DataPersistenceViewController.xib file, close it and go back to the Xcode.

Step 7: In the DataPersistenceViewController.m file make the following changes :

 

 

 

Step 8: Now compile and run the application in simulator. But remember you should type something in the textfield and press the Home button, otherwise if you exit the application, then your data will not save.

You can download source code from hereDataPersistance