7

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.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
mm24
  • 9,280
  • 12
  • 75
  • 170
  • See http://stackoverflow.com/questions/19127282/ibeacon-notification-when-the-app-is-not-running – danh Nov 18 '16 at 15:28

2 Answers2

6

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

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • Yeah, good suggestion. Unfortunately I need a deterministic method to mark when the app is about to be suspended. Playing with iBeacons and background app execution I noticed that the app gets "launched" only once even if multiple iBeacons are triggered with few minutes of distance between them. Background execution should be for at most 10 seconds. – mm24 Nov 23 '16 at 15:09
  • You could set a specific timer to execute code, say, 100 ms before the app is to be suspended. Without an OS callback, this is as close as you can get. – davidgyoung Nov 25 '16 at 06:43
3

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.

kamwysoc
  • 6,709
  • 2
  • 34
  • 48
  • 8
    Got the paradox. However a method like "applicationWillEnterSuspendedState" would make sense. – mm24 Nov 23 '16 at 15:07
  • 1
    I agree that method would be useful – kamwysoc Nov 23 '16 at 15:08
  • It looks like that there is no official way to do this.. I am not 100% convinced that hacking it is a robust approach for what I am trying to achieve. Will need to think about this more. – mm24 Nov 23 '16 at 15:11