What is the best way in core location to get background location updates only when there is change in country?
-
You can create the country's geofencing area and catch it in `didEnterRegion` – Quoc Nguyen Aug 06 '18 at 08:04
-
It can be any country, how do I achieve in this case? – Kalai Aug 06 '18 at 08:22
-
If you has no data about it, I think you can only check the country every the locationmanager update the new location data. https://stackoverflow.com/questions/8534496/get-device-location-only-country-in-ios. – Quoc Nguyen Aug 06 '18 at 08:33
-
see this for step by step tutorial : https://developer.apple.com/documentation/corelocation/getting_the_user_s_location/handling_location_events_in_the_background – Anbu.Karthik Aug 06 '18 at 09:04
2 Answers
You can use the reverseGeocodeLocation
of the CLGeocoder
to get the current country for your location.
func locationManager(_ manager: CLLocationManager, didUpdateLocations objects: [CLLocation]) {
let location = objects.last!
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location!) { (places, error) in
if(error == nil){
if(places != nil){
let place: CLPlacemark = places![0]
let country = place.country
// do something if its changed
}
} else {
//handle error
}
But the issue will be you need to be monitoring location for this to happen. You can use startMonitoringSignificantLocationChanges
as one option or you could set desired accuracy to something big like kCLLocationAccuracyThreeKilometers
both of which will reduce the amount of power used by location updates.

- 1,465
- 1
- 10
- 20
What you are looking for is called geofencing, there are great articles about it. Any way you'll need to implement functions like
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion)
(CLLocationManagerDelegate)
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion)
(CLLocationManagerDelegate)
open func requestAlwaysAuthorization()
(CLLocationManager)
func startMonitoring(for region: CLRegion)
(CLLocationManager)

- 201
- 1
- 5