Managing the Background State

To allow your application to continue to safely run when it goes into the background, you must implement code in its AppDelegate class to ensure that the connection to the server shuts down gracefully when going into the background, and starts up when the application becomes active again.

This is important because in iOS, when an application goes into the background, it can have its network sockets invalidated, or the application may be shut down at any time. For correct behavior of the AppDelegate connection, the connection needs to be stopped when in background, and only started again when the application goes back to the foreground.

You must implement two appDelegate methods: applicationWillResignActive and applicationDidBecomeActive.

Note: The applicationDidBecomeActive method is also called when the application first starts up, where most applications would have code already to register the application and start the AppDelegate connection. This example code uses a boolean wasPreviouslyInBackground so that the applicationDidBecomeActive method can detect whether it is called on coming out of the background or is called on a first startup.
Important: This example code does not work unless you have a patch. Contact the support organization to obtain the appropriate patch.
BOOL wasPreviouslyInBackground = NO;

- (void)applicationWillResignActive:(UIApplication *)application {
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
    
    if([SUPMessageClient status] != STATUS_NOT_START)
        [SUPMessageClient stop];
    
    wasPreviouslyInBackground = YES;
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
    if(wasPreviouslyInBackground)
        [SUPMessageClient start];