0

I'm writing an application that runs in background mode that can track user (so sometimes updates location in background mode) and, by using region monitoring, informs him of nearby points. I do not use the feature of relaunching the application from region monitoring.

Currently, after getting the applicationWillTerminate: message, I stop location updates and remove all points from region monitoring. I noticed strange behaviour of the application. The following messages (in order like below) are delivered to the application after trying to kill it in multitasking menu:

  • applicationWillTerminate:
  • applicationDidEnterBackground:
  • (sometimes) one more locationManager:didUpdateToLocation:fromLocation:

If I don't register background location mode for this application, it is just killed instead.

I'd like application to terminate like other applications after user kills it in the multitasking menu and not get messages from region monitoring (no points are tracked at that moment anyway). Still, I need to be able to use background location mode. The purpose is to minimize the usage of battery. What should I do to achieve that?

Also, why does the application receive applicationDidEnterBackground: after applicationWillTerminate:? Is it still running or not after shutting it down from multitasking menu?

Xilexio
  • 1,178
  • 19
  • 40

2 Answers2

2

In the end I tested it experimentally and checked if everything that is covered in documentation is true. It seems it is. Those are the results, that aren't fully specified in documentation:

If the application doesn't support UIBackgroundModes like location, it is killed (receives signal 9) after terminating it in multitasking menu (after receiving applicationWillTerminate:).

If the application supports location background mode, it receives applicationWillTerminate:, applicationDidEnterBackground: and is suspended. Before next launch of the application, it is silently terminated.

If you're monitoring a region in your application and it was terminated in multitasking menu, it will be relaunched on region events with UIApplicationLaunchOptionsLocationKey option. If user launches the application after that, it isn't terminated but simply starts getting the applicationDidBecomeActive: and other messages.

As expected, the application isn't draining the battery after being terminated, if you stop monitoring all regions in applicationWillTerminate: method. Though, if you'd not stop monitoring regions and had high region monitoring accuracy set, then it is draining a lot of battery, even if terminated (since iOS is actively monitoring those points then).

Behaviour of the application with set background location mode, after termination, is actually the same, whether you monitor regions or not. Only that in first case it just won't ever get messages about entering region nor drain the battery.

Xilexio
  • 1,178
  • 19
  • 40
0

It will always receive those notifications (and therefore enter those methods) as those notifications will ALWAYS be sent as part of the kill process of an application. And yes the application is STILL running when those methods run, but it will no longer be running after the applicationWillTerminate method ends.

If i understand what you are trying to accomplish then you should simply stop the monitoring depending on your requirements in the applicationDidEnterBackground: method.

Hope that helps.

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
  • The problem is that the application IS running after `applicationWillTerminate:`. After it, `applicationDidEnterBackground:` is called. The question is when (if ever) it's killed. Also, I want to stop monitoring regions once user "kills" the application, so I can't do it in `applicationDidEnterBackground:`. – Xilexio Aug 22 '12 at 17:05
  • 1
    According to the docs for iOS 4.0 and above your applications is purged after applicationDidEnterBackground, please refer to the docs here: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html – Oscar Gomez Aug 22 '12 at 17:14
  • It only states that application may be terminated after `applicationDidEnterBackground:`. For example, if I'd use region monitoring, it'd be ran with `application:didFinishLaunchingWithOptions:` with option `UIApplicationLaunchOptionsLocationKey`. Since I'm not using its functionality to wake up the application on crossing the region's borders, I'd expect it to be closed or at least not use up the battery. But, as I checked with System Console app, my app isn't being sent the kill (9) signal, while without background mode set, it is killed. – Xilexio Aug 22 '12 at 18:40