Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call viewWillDisappear when user quits application ios

Tags:

ios

I have an application that stores values in the viewWillDisappear method in one of my classes. For example, I store the data and time in the viewWillAppear method and the same again when in the viewWillDisappear method so I'm able to compare the time difference and log this out.

However, if the user presses the home button on the device, then the viewWillDisappear code is not run. I've tried to call this particular method in the AppDelegate's applicationDidEnterBackground, but this stores the wrong information as the viewWillDisappear method isn't actually run. I've also tried to store them in NSUserDefaults, but again, as the viewWillDisappear method isn't run, then the wrong values are stored.

Is there a way to make the viewWillDisappear method run for that view as soon as the user presses their home button?

like image 590
Adam Altinkaya Avatar asked Nov 07 '13 14:11

Adam Altinkaya


1 Answers

In the viewWillAppear register for this notification...

-(void)viewWillAppear:(BOOL)animated {

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(appEnteredBackground:)
                                             name: UIApplicationDidEnterBackgroundNotification
                                           object: nil];

}

-(void)viewWillDisappear:(BOOL)animated {

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

-(void)appEnteredBackground:(NSNotification *)appEnteredBackgroundNotification {

    //do your thing

}
like image 168
digthewells Avatar answered Sep 20 '22 00:09

digthewells