Get location updates in background - only for Country change iOS

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Get location updates in background - only for Country change iOS



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 6 at 8:04



didEnterRegion





It can be any country, how do I achieve in this case?
– Kalai
Aug 6 at 8:22





If you has no data about it, I think you can only check the country every the locationmanager update the new location data. stackoverflow.com/questions/8534496/….
– Quoc Nguyen
Aug 6 at 8:33





see this for step by step tutorial : developer.apple.com/documentation/corelocation/…
– Anbu.karthik
Aug 6 at 9:04




2 Answers
2



You can use the reverseGeocodeLocation of the CLGeocoder to get the current country for your location.


reverseGeocodeLocation


CLGeocoder


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.


startMonitoringSignificantLocationChanges


kCLLocationAccuracyThreeKilometers



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, didEnterRegion region: CLRegion)



func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) (CLLocationManagerDelegate)


func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion)



open func requestAlwaysAuthorization() (CLLocationManager)


open func requestAlwaysAuthorization()



func startMonitoring(for region: CLRegion) (CLLocationManager)


func startMonitoring(for region: CLRegion)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard