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?
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With