-1

I'm trying to write an app that notifies the user whenever he enters a specific region (I have 20 regions that being updates base on the user's current location).

I added a breakpoint on -locationManager: didEnterRegion: but it's never been called although the user's current location is inside the region (using the simulator).

It will really help me if someone will be able to help me detect the problem.

Thanks!

My code:

remindersViewController.m:

-(void)startMonitoringClosestStores
{
    [self stopMonitoringStores];

    if (![CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]]) {
        NSLog(@"Monitoring is not available for CLCircularRegion class");
}

for (Store *currentStore in self.twentyClosestStores) {
    CLCircularRegion *region=[currentStore createCircularRegion];
    [self.locationManager startMonitoringForRegion:region];
    }
}
-(void)stopMonitoringStores
{
    for (Store *currentStore in self.twentyClosestStores) {
        CLCircularRegion *region=[currentStore createCircularRegion];
        [self.locationManager stopMonitoringForRegion:region];
    }
}
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    NSLog (@"Enetred"); //Never called
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    self.userLocation=[locations lastObject];
    [self sortClosestStores];
}

Store.m:

-(CLCircularRegion*)createCircularRegion
{
    CLCircularRegion *region=[[CLCircularRegion alloc] initWithCenter:self.geoPoint radius:1000 identifier:self.identifier];

    region.notifyOnEntry=YES;

    return region;
}

AppDelegate.m:

-(void)handleRegionEvent:(CLRegion*)region
{
    NSLog(@"Geofence triggered");
}

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    NSLog(@"Entered");
}
Wain
  • 118,658
  • 15
  • 128
  • 151
Yhper
  • 183
  • 2
  • 14
  • are the remaining methods called ? Did u simulate the current location in simulator ? – Teja Nandamuri Jan 22 '16 at 16:00
  • 1
    PLease note that simulator cannot detect your current location. You have to simulate the location simulator. – Teja Nandamuri Jan 22 '16 at 16:04
  • @Mr.T Actually, now I see that the other delegate method (of CLLocationManager) hasn't been called as well. But `self.locationManager.delegate` is set to `self`. So what can be the problem? – Yhper Jan 22 '16 at 16:07
  • did u implement the delegate in the interface section ? it should be something like – Teja Nandamuri Jan 22 '16 at 16:08
  • @Mr.T Yes I did (on the .h file). – Yhper Jan 22 '16 at 16:11
  • did u enter the required keys in you plist file ? – Teja Nandamuri Jan 22 '16 at 16:12
  • look at this http://matthewfecher.com/app-developement/getting-gps-location-using-core-location-in-ios-8-vs-ios-7/ – Teja Nandamuri Jan 22 '16 at 16:14
  • @Mr.T I know it works because the map shows the user's current location correctly. – Yhper Jan 22 '16 at 16:15
  • did u enter the custom location in the simulator ? Else there is no way simulator to display the current location. – Teja Nandamuri Jan 22 '16 at 16:16
  • @Mr.T Yes I did. It works on both simulator and actual device. – Yhper Jan 22 '16 at 16:20
  • Is the user in the region to start, or do they enter the region at some later point? My vague recollection was that being in a region doesn't trigger the event, you have to cross the threshold - which is more or less consistent but these are not *always* caught 100% of the time. – Mike Jan 22 '16 at 16:26
  • A way to test this would be to start them in one location, and simulate their location THEN to inside the region - which *should* trigger the event. – Mike Jan 22 '16 at 16:26
  • @Mike the user enters the region later (not on the start) – Yhper Jan 22 '16 at 16:31

1 Answers1

0

but it's never been called although the user's current location is inside the region (using the simulator).

  1. Don't use the Simulator. Test region monitoring only with a device. You need to physically go places and test.

  2. Geofencing does not involve where you are. It involves imaginary lines (circles) that you cross. If the user is already inside the circle, there won't be a notification; if you have asked only for "on enter" notifications, the user would need to leave the circle and come in again, in order to be notified.

  3. This code could be problematic:

    -(void)startMonitoringClosestStores
    {
         [self stopMonitoringStores];
    

    Basically you are stopping before you even get started.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I'm cleaning all the regions and updating it with the new ones (becuase CLLocationManager can't monitor more than 20 regions at once, so I update the regions on didUpdateLocation – Yhper Jan 22 '16 at 16:33
  • While you must use device for geofence testing, it is not necessary to "physically go places". You can simulate your walk through on a device tethered to Xcode. The simulated geofencing is kind of "ideal", that's not what happens in real world, but still sufficient for smoke tests. Just be prepared that not all fence crossings will hit entry/exit events in field test. – Alex Pavlov Jan 22 '16 at 17:06
  • @AlexPavlov but the delegate methods are not being called at all – Yhper Jan 22 '16 at 17:18
  • @Yhper Because you turned them off (my point #3) before they could get started. – matt Jan 22 '16 at 19:37
  • @matt that's not the problem, even didUpdateLocation does not being called – Yhper Jan 23 '16 at 10:02
  • Why would it be? You never tell the location manager to begin updating locations. Your code makes no sense. Stop telling me what I'm saying is not the problem. I know how to do region monitoring. Do you? – matt Jan 23 '16 at 16:36
  • @matt you can use .gpx files instead of physically going to places. For some use cases that would be impossible. – Sarp Başaraner Mar 26 '19 at 14:10