Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: how to detect when the UIApplicationDelegate state becomes "suspended"?

How can we detect when an iOS App has been suspended?

There is no method that mentions this in the official UIApplicationDelegate documentation.

These are the states that an App can have:


(source: apple.com)

Use case:

I want to log when an app stops running subsequently to being woken up due a location event. For example I have got an iBeacon that the app is montioring. I activate the iBeacon and the app gets launched successfuly in background (for 10 seconds). I would like to detect when the App stops running after these 10 seconds have elapsed. However there is no AppDelegate method that seem to allow to intercept this (please consider that I am investigate this specific case.

Previous question: I had asked a previous similar question which did not get answered. Please find it here.

like image 403
mm24 Avatar asked Nov 18 '16 14:11

mm24


People also ask

What is suspended state in iOS app?

Inactive means that the app is still running in the foreground but it's not receiving events. Backgrounded: In this state, your app is not in the foreground anymore but it is still able to run code. Suspended: Your app enters this state when it's no longer able to run code.

Which method is called when app is killed iOS?

applicationWillTerminate(_:) — this method is called when the app is about to be terminated and removed from memory. It is always called for the apps that do not support background mode.

What is the meaning application suspended?

Ans: Suspended Process are those process which has been turned off temporarily. When a process is temporarily suspended then later on it will restart from exactly the same state where it was stopped. So, the state of those processes must be stored somewhere else on your PC.


2 Answers

While I am unaware of any callback, you can query for the amount of background time remaining with:

NSLog(@"background time remaining: %8.2f", [UIApplication sharedApplication].backgroundTimeRemaining);

Theoretically, you can put this in a thread and execute custom code a second or so before your app terminates. See my blog post here for more info:

http://developer.radiusnetworks.com/2014/11/13/extending-background-ranging-on-ios.html

like image 51
davidgyoung Avatar answered Sep 27 '22 17:09

davidgyoung


I think you won't get any feedback form Suspended state. Suspended means that app is in memory but no code is executing right now.

Documentation:

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.

So in my understanding, if an app would give you a callback with something like applicationDidEnterSuspendedState it will be a paradox, cause Suspended state means that no code is executed.

like image 31
kamwysoc Avatar answered Sep 27 '22 15:09

kamwysoc