Search code examples
objective-cnsnotificationcenteruiapplicationdelegateuiapplication

applicationDidEnterBackground and applicationWillTerminate both called when user swipes up to quit app


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.


Solution

  • 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"];
    }