iOS - CoreLocation and geofencing while app is closed
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
iOS 7.1 CoreLocation and geofencing when app is closed
I'm not sure about iOS7, but as of iOS8, 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.
That means geofence will work even when the app is terminated. Also, the callback is in appDidFinishLaunching, where you get the info dictionary in the key UIApplicationLaunchOptionsLocationKey
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.
iOS Geofencing with CoreLocation
Regarding your issue about monitoring in the background, please refer to Apples documentation, it states the following:
If a region boundary is crossed while an app isn’t running, that app is relaunched into the background to handle the event. Similarly, if the app is suspended when the event occurs, it’s woken up and given a short amount of time (around 10 seconds) to handle the event.
This means, as long as your authorizationStatus is set to .authorizedAlways then the rest will take care of it'self automatically.
link to rest of docs: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html#//apple_ref/doc/uid/TP40009497-CH9-SW1
Location updates when app is closed
I would recommend using region monitoring. The way this works is you set up a coordinate with a defined radius (region) an register it as a region to be monitored and the app will tell you when the user has enter or exited that region.
You can read all about it here: Region Monitoring
Related Topics
Block_Release Deallocating UI Objects on a Background Thread
Customize Apps Gallery in Appstore Page
Use Different Googleservice-Info.Plist for Different Build Schemes
Swift: Determine iOS Screen Size
Cabasicanimation Resets to Initial Value After Animation Completes
What Are the Differences Between a Uiview and a Calayer
How to Force a Uitableview to Hide the Separator Between Empty Cells
After Updating to Xcode 10.2: Invalid Toolchain Error When Trying to Submit App to App Store
How to Send Data from iPhone to Apple Watch in Os2 in Objective-C
Xcode Warning When Using Mapkit and Corelocation
Error: "Array Index Out of Range" in Multidimensional Array
How to Enable "Tap and Slide" in a Uislider
How to Get Email Id of User Using Facebook Sdk 4.7 in iOS 9
Go VS. Return Button in iOS Keyboard for HTML Input Forms
Timestamp Function That Has Been Working Reliably Just Caused Exc_Bad_Instruction