tags:

views:

31

answers:

2

hi I have set,

This is my below code in applicationwillterminate method.

NSString *string4 = @"Objective-C";

NSUserDefaults* defs7 = [NSUserDefaults standardUserDefaults];
[defs7 setObject:string4 forKey:@"subject_textfield"];

This is my below code in applicationdidfinishlaunching method.

NSUserDefaults* defs7 = [NSUserDefaults standardUserDefaults];
subject_textfield.text = [defs7 stringForKey:@"subject_textfield"];

but,when i press home and then i again launch application at that time i'm getting nil value for subject_textfield.text.

Why is it so?

A: 

Use [[NSUserDefaults standardUserDefaults] synchronize]; to enforce that NSUserDefaults are getting saved.

Reference:

Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes.

http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html#//apple_ref/doc/uid/20000318-CIHDDEGI

Henrik P. Hessel
+1  A: 

applicationwillterminate may not be called in iOS4. If you are on iOS4, check out the following events:

didFinishLaunchingWithOptions
applicationWillResignActive

applicationWillEnterForeground
applicationDidBecomeActive
ohho