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

Leave a Comment:

16 comments
Patrick says May 19, 2010

Thanks.

Of course, in a real application you wouldn’t want to assume the user knew to terminate the app to cause the data to be saved. So you’d put the writeToFile stuff someplace other then applicationWillTerminate.

Reply
Jepson says June 7, 2010

whom ever created this tutorial made me happy. I’ve struggled with this on my own with an astonishingly low success rate for a while.
with gratitude Jepson

Reply
dileep says June 18, 2010

pls help..

i’m new in iphone programming..

its not working in my own program..
i downloaded ur project… that too is not working…
the problem is control never comes to applicationWillTerminate:notification

i tried with ur project too..pls help..

thanks in advance

Reply
dileep says June 18, 2010

pls help..

i’m new in iphone programming..

its not working in my own program..
i downloaded ur project… that too is not working…
the problem is control never comes to applicationWillTerminate:notification

i tried with ur project too..pls help..

thanks in advance

Reply
    Frank says November 4, 2010

    For iOS 4.X you will need to use UIApplicationDidEnterBackgroundNotification

    Reply
Sushant says June 18, 2010

In our application whenever you lunch the application in the Simulator. Two textField will be display and you write on the textfield and press the home button and next lunch the application once again, then you will see the output,whatever you write in the textfield before press the HOME button.

Reply
dileep says June 18, 2010

thank you for ypur reply..

i did like wat u said.. it’s working if press home and check…but after that i quit the simulator but on the next launch again the textfields are blank..
tat means no data had written in to plist..

i put breakpoint at the applicationWillTerminate:notification … but control never come there..

i’m using simulator 4.0 ..is it bcz of tat..?

pls help.. thanks in advance..

Reply
dileep says June 18, 2010

thank you for ypur reply..

i did like wat u said.. it’s working if press home and check…but after that i quit the simulator but on the next launch again the textfields are blank..
tat means no data had written in to plist..

i put breakpoint at the applicationWillTerminate:notification … but control never come there..

i’m using simulator 4.0 ..is it bcz of tat..?

pls help.. thanks in advance..

Reply
Sushant says June 18, 2010

It’s very important that you press the home button. If you just quit the simulator,that means you force quitting your application , and you will never receive the notification that the application is terminating and your data will never get saved.

Reply
dileep says June 18, 2010

ya.. i’ve clicked HOME.. even on that time also control not going to applicationWillTerminate:notification function.. n nothing saved in plist.

writing to file command written in appwillterminate function is not callindg for a single time..

Reply
Sushant says June 18, 2010

You can try one think. Run this application on the actual device and see what is the output.

Reply
byungguk kang says June 23, 2010

i tested code with 4.0 simulator.
when i pushed home button, application not terminated but in sleep mode.

Reply
Roman K. says July 6, 2010

The published code doesn´t work with SDK 4.0. If you run it with 3.2, it will work quite fine. It´s working but the applicationWillTerminate function will be never reached until you change your SDK to 3.2. dileep is right and maybe someone could post a working notification example for SDK 4.0.

greetz
Roman

Reply
siroganes says July 9, 2010

i appreciate u r bloging…

but i cant use ur source….. bcuz the problem is control never comes to applicationWillTerminate:notification

i know ur source problum….

problum is viewDidload method….

———————————————-
– (void)viewDidLoad {

NSString *filePath =[self dataFilePath];
test1.text = @” viewdidload method filePath start!!”;

if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{

NSLog(@” viewdidload method filePath if part”);
test2.text = @” viewdidload method filePath if part”;
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
field1.text = [array objectAtIndex:0];
field2.text = [array objectAtIndex:1];
[array release];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification object:app];

[super viewDidLoad];
}

Reply
siroganes says July 9, 2010

i appreciate u r bloging…

but i cant use ur source….. bcuz the problem is control never comes to applicationWillTerminate:notification

i know ur source problum….

problum is viewDidload method….

———————————————-
– (void)viewDidLoad {

NSString *filePath =[self dataFilePath];
test1.text = @" viewdidload method filePath start!!";

if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{

NSLog(@" viewdidload method filePath if part");
test2.text = @" viewdidload method filePath if part";
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
field1.text = [array objectAtIndex:0];
field2.text = [array objectAtIndex:1];
[array release];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification object:app];

[super viewDidLoad];
}

Reply
Mariz Melo says August 22, 2010

The problem is relate to the SDK 4, in this SDK the application will terminate like a Forcing quite… than the method WillTerminate is not called.

To fix the problem just use the method “applicationDidEnterBackground” instead.

To change just replace “applicationWillTerminate” for “applicationDidEnterBackground”, that’s it.

MM

Reply
Add Your Reply