43

I've read many tutorials about geofencing my iOS app (i.e., here), but none mention whether or not iOS allows any app-specific location data to be handled when the app is closed.

For instance, I understand that when the app is in the background, these services will still persist (if coded correctly), but how about when the user double taps the home button and closes the app? Can location data still be obtained?

Daniel
  • 22,521
  • 12
  • 107
  • 150
Brett
  • 10,971
  • 27
  • 114
  • 198
  • yes it can still be obtained. As long as the app is kept running in the background and provided programmer has coded in such a way as to send location updates to your backend. – Srikar Appalaraju Jun 18 '13 at 16:28
  • Once you terminate the application via double tap on the home button your out of luck. – ninehundreds Jun 18 '13 at 16:28
  • Actually, you should revisit the docs. See my answer below. http://stackoverflow.com/a/17174344/662605 – Daniel Jun 18 '13 at 16:43

4 Answers4

67

According to the Apple Documentation, in the Using Regions to Monitor Boundary Crossings section:

In iOS, the regions you register with the location manager persist between launches of your application. If a region crossing occurs while your iOS app is not running, the system automatically wakes it up (or relaunches it) in the background so that it can process the event. When relaunched, all of the regions you configured previously are made available in the monitoredRegions property of any location manager objects you create.

So yes, your application will be woken up (or relaunched!) when the system's location detects you entered/exited (depending on your setup) a desired region, so this is even if your app isn't running of course. You just need to handle it correctly in the application delegate, when the app is relaunched you get passed a UIApplicationLaunchOptionsLocationKey key in the options dictionary. See documentation link below for details.

Please remember that the -startMonitoringForRegion:desiredAccuracy: method is deprecated in iOS 6, so it shouldn't be used. Instead use -startMonitoringForRegion.

To know how to handle when your app is relaunched following a location event, see documentation here, that info as you will see is in the discussion of the deprecated method but it should still be relevant, I believe Apple forgot to migrate this information to the new method when they deprecated the old one. I've filed a bug to them about it.

UPDATE

Apple have updated the documentation of CLLocationManager following my bug report. Documentation now specifies for which types of location monitoring the app is or isn't launched after having been terminated. See Using Location Services in the Background

Daniel
  • 22,521
  • 12
  • 107
  • 150
  • Great comments! This is exactly what I couldn't find :-) (Probably looking in the wrong place). Thanks!. – Brett Jun 18 '13 at 17:38
  • If that info about the app delegate was still on the main `CLLocationManager` documentation page then probably you would have seen it ;) – Daniel Jun 18 '13 at 17:39
  • 19
    For anyone that finds this, unfortunately in iOS7 it is no longer true. If the user force quits the application by double-tapping home, and sliding up in the multitasker, there is NO way to get relaunched. To quote pmarcos from the Apple forums 'As we talked about in the What's New with Multitasking session at WWDC in June, this is changing for iOS 7. When a user kills an app in iOS 7, the OS will not relaunch it for any reason.' – pj4533 Oct 11 '13 at 01:08
  • @pj4533 thanks, this is what I missed. This is bad, how does it suppose to work then? doc's says it will relaunch terminated applicaiton. – Marcin Oct 30 '13 at 17:06
  • 2
    Marcin, Apple engineers said at WWDC that because _all_ apps can get launched in the background on iOS 7, do some stuff, update their snapshot and go to sleep again, it would be weird for an app you explicitly quit to get woken up like every hour or so. To be consistent, apps users explicitly quit will now never get woken up in the background as pj4533 said. – wander Dec 09 '13 at 18:06
  • 1
    iOS 7.1 was supposed to change this behavior. I.e., ios 7.1 was supposed to relaunch your app even if it wasn't running in the background. But I can confirm that this is still a problem. This is not limited only to the third-party apps. Even apples native apps such as "find friends" or the Reminder have this problem in iOS 7.x. – RawMean Apr 12 '14 at 05:37
  • Is this still true for iOS8, I'm using -startMonitoringForRegion. but when terminated by user the app is not restarted despite Apple saying otherwise: goo.gl/Q0Bo8W. "One exception is location apps, which in iOS 8 and later are relaunched after being force quit by the user." – Mahakala Nov 13 '14 at 14:32
  • @pj4533 and anyone else who is here might find this useful - According to Apple Docs, "The region monitoring service delivers events normally while an app is running in the foreground or background. (You can use this service for both geographic and beacon regions.) For a terminated iOS app, this service relaunches the app to deliver events. Use of this service requires “Always” authorization from the user". – Gautam Jain Jul 10 '15 at 12:07
  • @Daniel do you have any examples for do the region monitoring in background also – Mahesh reddy Feb 29 '16 at 12:49
  • @GautamJain Have you checked if app gets silently launched for getting location? My requirement is to get user's location (in killed state) and send it to server. – Nikhil Manapure Apr 11 '17 at 13:24
  • @NikhilManapure - Yes, it works, only if you have always authorization and not only when in use. You can also try geofencing. Register a geofence, hit it and the app will launch in the background even if it is killed. Again, you need always authorization. – Gautam Jain Apr 13 '17 at 08:08
  • @GautamJain Thanks a lot, will surely try these stuff. – Nikhil Manapure Apr 13 '17 at 09:44
  • It usually takes ages to get a notification when the app is closed. Is there a way to decrease it? – SuperJMN Jun 22 '17 at 14:30
2

Some forms of location update require the app to be running in the background, some don't. If you want 'constant' location updates then the app needs to be running in the background. If you want only significant changes (and I think it works for region monitoring too, though the docs aren't quite so explicit) then the app will be relaunched if it was terminated:

If you start this service and your application is subsequently terminated, the system automatically relaunches the application into the background if a new event arrive...

(from CLLocationManager docs)

Wain
  • 117,132
  • 14
  • 131
  • 151
0

If you use region monitoring capabilities your can resume your app somehow even if is closed, usually by creating a local notification, in this way user can renter in the application and do specific task, by watching the options dictionary in -applicationDidFinishLaunchingWithOptions:

Andrea
  • 24,774
  • 10
  • 79
  • 121
0

There are services that relaunch the app to report location updates even when the app has closed.
The following documentation is helpful. https://developer.apple.com/documentation/corelocation/getting_the_user_s_location/handling_location_events_in_the_background#2865362

enter image description here

tikidunpon
  • 21
  • 2