IOS: How to determine when an application will be suspended?

I want to know when my application will be suspended? Inactive state for a certain time or termination by the user. I need this because I need to close the connection with a web socket. I want to keep the connection alive while the application is in the background.

How to do it?

thanks

EDIT:. This is not a duplicate question, other questions want when the application ceases to be active, I would like to know that the application was interrupted.

+7
ios objective-c application-state background
source share
3 answers

In your AppDelegate.m file, this method will be called when the user clicks the "Home" button and the application goes into the background (here you can save your connection live, but you have to read the apple documentation regarding background tasks because your application does not can live forever if the application remains in the background, there are other ways to update your application, such as updating in a push notification, etc.):

- (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. } 

and this method will be called when the application is completed (completely closed from multitasking).

 - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } 

You can process your connections with these methods.

-one
source share

You can also add a notification observer

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveSuspendNotification:) name:UIApplicationWillResignActiveNotification object:nil]; - (void) receiveSuspendNotification:(NSNotification*)notif { } 
Method will be called

and you can complete the required tasks.

+3
source share

if your application has not registered in the background, then upon receipt of UIApplicationDidEnterBackgroundNotification your application will be suspended in RAM.

-2
source share

All Articles