iOS: How to tell when an app is going to suspend? [duplicate]
Asked Answered
C

3

10

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

Caveator answered 13/12, 2013 at 6:7 Comment(4)
@dandan78 that is only for entering the background, I want a notification for when the app is suspended.Caveator
This should be re-opened. Suspended is not the same as Not active (Backgrounded) - gist.github.com/BadPirate/0a480b947744c8c0e326daa4ab479b09Swordsman
Possible better duplicate (and answer) https://mcmap.net/q/989980/-how-can-i-tell-if-my-app-has-suspendedSwordsman
Suspend is not background so the duplicate tag should be removed!Monocyclic
L
-4

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.

Lawmaker answered 13/12, 2013 at 6:10 Comment(7)
"Suspend" is not "Background".Cesaria
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.Epizoic
@jeraldo No, you have no way to know when your app moves from Background state to Suspended state. iOS doesn't "kill" apps, see developer.apple.com/library/ios/documentation/iPhone/Conceptual/…Sayer
This is totally wrong. This tells when your app is backgrounded, not when it is suspended.Chukchi
NO. This is NOT when the app becomes suspended. This catches when the app enters the background.Areola
@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. developer.apple.com/documentation/uikit/uiapplicationdelegateElectrophilic
@Electrophilic Yeah I know. I was meant that iOS doesn't always kill apps as soon as they become suspended.Sayer
K
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.

Katzenjammer answered 13/12, 2013 at 7:27 Comment(4)
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.Theravada
I find notifications to be a cleaner approach if you're doing something somewhere other than the app delegate.Karyolysis
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.Areola
This is for Inactive state, suspended is different from Inactive.Strategic
S
-2

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

Shaniceshanie answered 13/12, 2013 at 9:32 Comment(0)
L
-4

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.

Lawmaker answered 13/12, 2013 at 6:10 Comment(7)
"Suspend" is not "Background".Cesaria
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.Epizoic
@jeraldo No, you have no way to know when your app moves from Background state to Suspended state. iOS doesn't "kill" apps, see developer.apple.com/library/ios/documentation/iPhone/Conceptual/…Sayer
This is totally wrong. This tells when your app is backgrounded, not when it is suspended.Chukchi
NO. This is NOT when the app becomes suspended. This catches when the app enters the background.Areola
@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. developer.apple.com/documentation/uikit/uiapplicationdelegateElectrophilic
@Electrophilic Yeah I know. I was meant that iOS doesn't always kill apps as soon as they become suspended.Sayer

© 2022 - 2024 — McMap. All rights reserved.