How to properly stop the application when the user minimizes the application? I have this situation. There is a splash screen and after it I request permission for location from the user and after that I request permission for notification

So, if you start the application and immediately press the Home button, the application is minimized, but in the background it continues to perform all the same actions as if it is not minimized (a dialog appears asking for the first permission, then after it the request for the second and I see all activity in the logs)

I understand that in AppDeligate you need to track when the application is minimized and put all execution on pause, but I don’t quite understand how to do it.

How to track when the program is running at the time of folding?

Or just kill all the activity and then when opening the application, check where it was stopped? But then it needs to be done for all screens and stopping the execution of all screens as it is to make in AppDeligate (as for me it’s not a completely smooth solution)

Or I do not understand how this should work?

  • Where do you ask permission for location and notification? Is this performed somewhere in didFinishLaunchingWithOptions? - Vitali Eller
  • @VitaliEller I don’t do it in a separate class ... By the way, I’m not sure if this is correct, because according to the tutorials, this should be done in AppDeligate, but in that case the request will be at the start of the application, but I need to ask permission on the second screen, but not immediately ... Therefore, I brought these methods to another class and use them where necessary ... What do you think? - Aleksey Timoshchenko
  • Well, this is a normal decision, if you need to ask a little later. I'm just trying to figure out how this method is called when the user presses the Home button. What happens in the background? Rekvest? Opening a view? - Vitali Eller
  • @VitaliEller this way, the application opens, splash the screen for 2 seconds and after 2 seconds the Timer opens the next screen, when you open the screen a request is made for the location and in the cooler after I receive a response from the user by location I call the request for notifications. .. like this. But the problem as I described when running on the splash screen. If you minimize the application, then all actions with permission requests still occur - Aleksey Timoshchenko
  • Yes, now I see. The application switches to the background state, and the timer does not stop, you need to do some method that will stop the timer and listen to the notification about the transition of the system to the background. As soon as you receive the notification, turn off the timer. Then everything should work as it should. - Vitali Eller

1 answer 1

Use the viewDidAppear method instead of viewDidLoad . viewDidLoad works as soon as a command to switch to another viewDidLoad occurs. viewDidAppear triggered when a view has already appeared on the screen. When the application goes into the background, it executes the code, but does not do everything related to the graphics. And asking for permission is not a graphic per se.