mm24
mm24

Reputation: 9586

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.

Upvotes: 7

Views: 2961

Answers (2)

kamwysoc
kamwysoc

Reputation: 6859

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.

Upvotes: 3

davidgyoung
davidgyoung

Reputation: 64926

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

Upvotes: 6

Related Questions