Lolloz89
Lolloz89

Reputation: 2809

applicationWillResignActive notification uncaught on device

I'm trying to save a simple string on a simple .plist file when the application is closing.

This is my code:

- (void)viewDidLoad {
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
    NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
    self.kmOld.text = [array objectAtIndex:0];

    [array release];
}


[[NSNotificationCenter defaultCenter] addObserver:self
                                      selector:@selector(applicationWillResignActive:)
                                      name:UIApplicationWillResignActiveNotification 
                                      object:NULL];
[super viewDidLoad];

}

Then follow the applicationWillResignActive method:

 - (void)applicationWillResignActive:(NSNotification *)notification {

    NSString *text = [[NSString alloc]initWithFormat:@"%@",kmNew.text];
    NSLog(@"%@",text);

    if ([text intValue]>0) {
        NSArray *array = [[NSArray alloc] initWithObjects:text, nil];

      BOOL success = [array writeToFile:[self dataFilePath] atomically:YES];
        NSLog(@"%d",success);
        [array release];
    }
    [text release];
}

This work fine on the simulator, but it seems to be ignored by the device... Where is the problem? Thanks...

Upvotes: 2

Views: 1892

Answers (1)

Jonah
Jonah

Reputation: 17958

Take a look at http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/ for a good overview of the UIApplication lifecycle. Depending on the iOS version your app is running on and the action causing your app to terminate/background you may not be listening for the correct notification and may need to observe UIApplicationWillTerminateNotification as well.

iOS <4

iOS 4+

Upvotes: 8

Related Questions