2

I working on demo for Goefencing.

Here's my code,

- (void) registerRegionForMonitoring {

    if (self.locationManager == nil) {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.distanceFilter = kCLDistanceFilterNone;

        if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [self.locationManager requestAlwaysAuthorization];
        }

        [self.locationManager startUpdatingLocation];
    }

    if(![CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]]) {
        [Utilities showAlertWithTitle:@"Geofence" andMessage:@"This app requires region monitoring features which are unavailable on this device."];
        return;
    }

    for(NSDictionary *dictionary in geofences) {

        NSString *geofenceId = [NSString stringWithFormat:@"%@", [dictionary valueForKey:@"geofence_id"]];
        CLLocationDegrees latitude = [[dictionary valueForKey:@"latitude"] doubleValue];
        CLLocationDegrees longitude =[[dictionary valueForKey:@"longitude"] doubleValue];
        CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);
        CLLocationDistance regionRadius = [[dictionary valueForKey:@"radius"] doubleValue];

        CLRegion *geofenceRegion = [[CLCircularRegion alloc] initWithCenter:centerCoordinate radius:regionRadius identifier:geofenceId];
        [self.locationManager startMonitoringForRegion:geofenceRegion];
   }
}

Here are the delegate methods

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

    CLLocation *latestLocation = [locations lastObject];
    self.currentLocation = latestLocation;
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    [Utilities showAlertWithTitle:@"Geofence" andMessage:[NSString stringWithFormat:@"You are in region with identifire %@", region.identifire]];
}

- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
    [self.locationManager requestStateForRegion:region];
}

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    [Utilities showAlertWithTitle:@"Geofence" andMessage:[NSString stringWithFormat:@"You are out of region with identifire %@", region.identifire]];
}

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {

    if (state == CLRegionStateInside) {
        //  if user already in the region, when monitoring is started
        [Utilities showAlertWithTitle:@"Geofence" andMessage:[NSString stringWithFormat:@"You are in region with identifire %@", region.identifire]];
    }
}

I have also added key "NSLocationAlwaysUsageDescription" in plist file for iOS 8+

Also below added background modes:

  • App registers for location updates

  • App shares data using CoreBluetooth

  • App communicates using CoreBluetooth

  • App downloads content in response to push notifications

Everything is working fine with iOS 7 and also working in iOS simulator for iOS 8.4

It is not working on the device (iOS 8.4, both iPad and iPod)

I have checked with wifi on.

I have also switched on 'General' -> 'Background App Refresh' in Settings

I don't know what am I missing?

All suggestions are welcomed. Thanks in advance.

AlBlue
  • 23,254
  • 14
  • 71
  • 91
Nirav Valera
  • 420
  • 4
  • 13
  • Can you explain how exactly you test the app when running on device? Is it a legwork or location simulated via Xcode (hint: results might be different) – Alex Pavlov Jul 13 '15 at 13:22
  • @AlexPavlov : thanks for immediate response. What i have done is, i have added current location coordinates with 1000m radius for creating region. Then added that region for monitoring. So, it should called determineState method. But it didn't. While same thing is working on my iOS simulator 8.4 – Nirav Valera Jul 14 '15 at 04:49

1 Answers1

0

Have you tried checking if the region really starts tracking? This method will return the error.

- (void)locationManager:monitoringDidFailForRegion:withError:

If you call start monitoringForRegion before you get a good position from the gps you will probably get an error.

Also, you might want to check the state CLRegionStateUnknown in the didDetermineState since that is an option as well.

Rabs G
  • 1,300
  • 1
  • 11
  • 16