Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - CoreLocation and geofencing while app is closed

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?

like image 793
Brett Avatar asked Jun 18 '13 16:06

Brett


People also ask

Does geofencing work without an app?

An app – very important to note that geofences for 99,9% of the use cases cannot be used without an app. There are some minor exceptions, but most likely, if you are reading this blog post, you will need an application – whether your own or through a partner – that can be utilized for this purpose.

How does geofencing work on iOS?

Geofencing notifies your app when its device enters or leaves geographical regions you set up. It lets you make cool apps that can trigger a notification whenever you leave home, or greet users with the latest and greatest deals whenever favorite shops are nearby.

Can you geofence on iPhone?

Setting up geofencing on an iPhone (iOS)Turn on Geofencing in the Home Center app (Application Settings > Geofencing). Go to the iPhone's Settings, scroll down and choose Home Center from the list. Tap Location and set Always (and make sure you have Precise Location enabled). Done.


2 Answers

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

like image 77
Daniel Avatar answered Sep 21 '22 03:09

Daniel


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)

like image 37
Wain Avatar answered Sep 18 '22 03:09

Wain