iOS: How to tell when an app is going to suspend?

Jason Silberman picture Jason Silberman · Dec 13, 2013 · Viewed 11k times · Source

I want to know when my app is going to be suspend? The state of not being active for a certain amount of time or being terminated by the user. I need this because I need to close a connection a web socket. I want to keep the connection alive while the app is in the background state though.

How do I do this?

Thanks

Answer

Keshav picture Keshav · Dec 13, 2013

You can also add Notification observer

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(receiveSuspendNotification:)
                                             name:UIApplicationWillResignActiveNotification
                                           object:nil];

- (void) receiveSuspendNotification:(NSNotification*)notif
{
}

method will get called and you can perform the required tasks.