2

I have a problem with handling Geofence and user current location at the same time.

When app starts working, didUpdateLocations called perfectly and I have the user location as desired.

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

But as soon as the didEnterRegion delegate being called,

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {}

the didUpdateLocations will not fire anymore. are they related together?

I also checked the

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {}

the status not changed after didEnterRegion gets called. I also added the

locationManager?.startUpdatingLocation()

in didEnterRegion but still the didUpdateLocations not gets called.

does anyone know where the problem is? How to track the location in the regions?

PS: updating location automatically stopped when EnterRegion is being called and automatically start when ExitRegion is being called

Update: I use xCode9, iOS 10, swift 4. both the EnterRegion and ExitRegion are empty and I just print a log in them now, they're going to post some data to the server in the future.

Mina
  • 2,167
  • 2
  • 25
  • 32
  • I've made an edit to my answer. Please since this seems very odd, I suggest you that you share your code for `didEnterRegion`, `didExitRegion` and what you call after you do `startMonitoring`. Also what iOS versions did you tested this on? – mfaani Dec 16 '17 at 14:10
  • I've updated my question with the answer of your questions. – Mina Dec 17 '17 at 04:59
  • @Mina I have selected 50 meters radius and 'upon exit' notification should come. But I am getting a notification on/around 250 meters and some time more than this. Please help me out. – Amanpreet May 16 '18 at 09:52
  • @Mina is it normal behavior of Geo fence did exit region? As I have tested the Raywendelich tutorial project and it is also working same as per my app. In ray's app, I have set radius 10 but I got notification around 250 meters. – Amanpreet May 16 '18 at 10:07

1 Answers1

2

the didUpdateLocations will not fire anymore. are they related together?

NO! See here

The didChangeAuthorizationStatus is meaningless here. The Authorization stays the same. It's just been stopped.

Normal tracking will not have any effect on regionMonitoring. That is managed by the OS.

Updates stop only when the app is suspended, thereby preventing the app from being woken up to handle those events.

My guess is that the moment you enter a region your app is no longer suspended and can start tracking again, so you have to make sure your app isn't getting suspended. To track that you could do use the UIApplication.shared.backgroundTimeRemaining. See this example

  • Avoid testing with the simulator. As the app life cycle is a little messed up . See this post from an Apple employee. You have to disconnect from your mac and use logging so you can have a true experience
  • Make sure you see this question. It walks you through some of geofence limitations, I'm not sure how much Apple has improved since then

But to be honest I still think you're just making a simple mistake and somehow end up getting your locationTracking stopped...

mfaani
  • 33,269
  • 19
  • 164
  • 293
  • well, I've already set the ```pausesLocationUpdatesAutomatically``` to false. – Mina Dec 16 '17 at 13:17
  • the weird thing is, updating location automatically stopped when ```EnterRegion``` is being called and automatically start when ```ExitRegion``` is being called. – Mina Dec 16 '17 at 13:19
  • Additionally what you said is impossible ie that as soon as your didEnter is called you stop tracking. You must somehow have a call to `stopUpdatingLocation`. Do a search on that in your entire project. If not then you have found an iOS bug. – mfaani Dec 16 '17 at 13:21
  • What is the distanceFilter you have set? Set it to something very low and try again – mfaani Dec 16 '17 at 13:23
  • 1
    it is none -> ```kCLDistanceFilterNone``` – Mina Dec 16 '17 at 13:24
  • Kheyli ajibe...but just give it a value. Sometimes defaults misbehave – mfaani Dec 16 '17 at 13:26
  • I'll do that and update you with the result, thanks for the answer. – Mina Dec 16 '17 at 13:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/161382/discussion-between-mina-and-honey). – Mina Dec 17 '17 at 05:06