1

I am adding some notifications for when the user sends the application to the background or when they completely quit the app. However when they quit the app, both the methods applicationDidEnterBackground and applicationWillTerminate are called. Why is this so and how can I just have applicationWillTerminate get called when the user quits the app?

This is objective-c for if anyone is wondering.

pnizzle
  • 6,243
  • 4
  • 52
  • 81
  • I have tested this phenomenon, as it appears to me, on a new and clean project. I get the same experience. So it has nothing to do with my code. – pnizzle Sep 02 '15 at 06:14
  • The main question is why is `didEnterBackgroundNotification` gets called on termination? When swiping and terminating, we expect `willResignActiveNotification` followed by `willTerminateNotification`. Did the App really move to background? I think (at some point in the past) this was not true. Writing CoreData was suggested in both 'background' and 'terminate' since killing the App 'will not go through the background'. – bauerMusic Sep 02 '23 at 04:08

2 Answers2

0

applicationWillTerminate method will be called only when application is not in suspended state while being terminated.

Following links may help you -

Which function is called when iPhone app is terminated?

applicationWillTerminate when is it called and when not

Community
  • 1
  • 1
nilam_mande
  • 931
  • 7
  • 14
  • Double tapping the home button to expose multitasking does not place the app in the background, I have tested this. When I then swipe up to quit the app both methods are called. Please give me a practical example of when `willTerminate` is called on its own. – pnizzle Sep 02 '15 at 05:34
  • Your response doesn't address my question anyway. I am not asking for when it is called or not called, I instead need to know why the two are called together when quitting and app and not `applicationWillTerminate` on its own... Or a solution to go around the issue – pnizzle Sep 02 '15 at 05:39
0

Seeing that applicationWillTerminate is called after applicationDidEnterBackground I have decided to set a notification.fireDate of 1sec from the point at which applicationDidEnterBackground is called. When applicationWillTerminate is called it first cancels the notification that was scheduled within applicationDidEnterBackground.

Pseudocode:

-applicationDidEnterBackground()
{
    [self scheduleLocalNotificationAtTimeIntervalFromNow:1.0f  identifier: @"someIdentifier"];
}

-applicationWillTerminate
{
    [self cancelLocalNotificationWithIdentifier: @"someIdentifier"];
    [self scheduleLocalNotificationAtTimeIntervalFromNow:0.0f  identifier: @"irrelevantIdentifier"];
}
pnizzle
  • 6,243
  • 4
  • 52
  • 81
  • This approach, though tacky, is working well for my scenario in the meantime. Better than getting a notification for both didEnterBackground and willTerminate. – pnizzle Sep 02 '15 at 06:34