Leave a Comment:
16 comments

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
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

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
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
For iOS 4.X you will need to use UIApplicationDidEnterBackgroundNotification
Reply
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
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
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
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
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
You can try one think. Run this application on the actual device and see what is the output.
Reply
i tested code with 4.0 simulator.
when i pushed home button, application not terminated but in sleep mode.

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

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];
}

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];
}

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