Call viewWillDisappear when user closes ios app

I have an application that stores values ​​in the viewWillDisappear method in one of my classes. For example, I store data and time in the viewWillAppear method, and also the same when in the viewWillDisappear method, so I can compare the time difference and write it down.

However, if the user clicks the home button on the device, the viewWillDisappear code viewWillDisappear not start. I tried calling this specific method in the AppDelegate applicationDidEnterBackground , but it stores incorrect information since the viewWillDisappear method is viewWillDisappear actually running. I also tried to save them in NSUserDefaults , but again, since the viewWillDisappear method viewWillDisappear not running, then the wrong values ​​are saved.

Is it possible to run the viewWillDisappear method for this view as soon as the user clicks his home button?

+6
source share
1 answer

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 } 
+8
source

Source: https://habr.com/ru/post/957594/


All Articles