9

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

nyedidikeke
  • 5,016
  • 7
  • 33
  • 49
Jason Silberman
  • 2,352
  • 6
  • 26
  • 45
  • 3
    @dandan78 that is only for entering the background, I want a notification for when the app is suspended. – Jason Silberman Dec 13 '13 at 21:21
  • 1
    This should be re-opened. Suspended is not the same as Not active (Backgrounded) - https://gist.github.com/BadPirate/0a480b947744c8c0e326daa4ab479b09 – BadPirate Jan 28 '20 at 19:24
  • Possible better duplicate (and answer) https://stackoverflow.com/a/59955755/285694 – BadPirate Jan 28 '20 at 19:30

3 Answers3

4

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.

Popeye
  • 10,831
  • 9
  • 52
  • 85
Keshav
  • 1,842
  • 1
  • 19
  • 28
  • Why add a notification observer to the notification center when you could use the methods in `NikosM.` answer. Seems a bit redundant adding another call in doesn't it. – Popeye Dec 13 '13 at 08:17
  • 1
    I find notifications to be a cleaner approach if you're doing something somewhere other than the app delegate. – zekel Feb 01 '16 at 18:26
  • http://stackoverflow.com/users/980097/popeye, because it doesn't work. As of 2017, this shows when the app moves into the background, but not when it becomes suspended several minutes *after* it has stayed in the background. – Alex Zavatone Mar 28 '17 at 18:28
  • 1
    This is for Inactive state, suspended is different from Inactive. – ViruMax Mar 25 '19 at 08:37
-2

In your AppDelegate.m file this method will be called when the user hits the home button and the app will go to the background (here you can keep your connection live, but you should read the apple documentation regarding background tasks, because your connection cannot be live forever if the app remains in the background. There are other ways to keep your app up to date like update on 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 app will get terminated (closed completely from multitasking).

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

You can handle your connections within this methods.

Nikos M.
  • 13,429
  • 4
  • 46
  • 60
  • 14
    "Suspend" is not "Background". – Almo Jun 16 '14 at 13:41
  • Can I just clarify since I am a little confused about this, does applicationWillTerminate get called when the app is in the background for a long time and iOS kills the app? I have an app that is being restarted after 10+ minutes in the background if you go back to it from the background. – SleepNot Jul 14 '14 at 03:03
  • 3
    @jeraldo No, you have no way to know when your app moves from Background state to Suspended state. iOS doesn't "kill" apps, see https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html – xaphod Apr 08 '16 at 14:47
  • 5
    This is totally wrong. This tells when your app is backgrounded, not when it is suspended. – Brennan Vincent May 31 '16 at 05:31
  • 1
    NO. This is NOT when the app becomes suspended. This catches when the app enters the background. – Alex Zavatone Feb 28 '17 at 21:09
  • @xaphod. iOS *does* kill your app if it gets under memory pressure or has other resource needs. From Apple: "The app is in memory but is not executing code. The system suspends apps that are in the background and do not have any pending tasks to complete. The system may purge suspended apps at any time without waking them up to make room for other apps." Purge = kill for all intents and purposes. https://developer.apple.com/documentation/uikit/uiapplicationdelegate – chadbag Dec 07 '17 at 20:50
  • @chadbag Yeah I know. I was meant that iOS doesn't _always_ kill apps as soon as they become suspended. – xaphod Dec 07 '17 at 23:44
-2

if your app did not register running in background, then when receiving the UIApplicationDidEnterBackgroundNotification your app will be suspended in RAM.

hippo
  • 77
  • 4