0

I'm making an app with GeoFencing. I've read here and built an app. It doesn't crash but neither does it react once I enter the monitored area. It does log out "Started monitoring" but it doesn't react once I select my *.GPX file. This is my ViewController code:

@interface myViewController () {
CLLocationManager *locationManager;
}

@end

@implementation RegisterViewController

@synthesize GeoFences;

- (void)viewDidLoad {
[super viewDidLoad];

// Initalize locationManager
locationManager = [[CLLocationManager alloc] init];
}

- (IBAction)getLocation:(id)sender {
// Start monitoring
// Create a home NSDictionary
NSDictionary *myDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"23123124arevar", @"title", @"00.0", @"latitude", @"00.0", @"longitude", @"100", @"radius", nil];
NSMutableArray *regions = [[NSMutableArray alloc] init];
CLRegion *region = [self mapDictionaryToRegion:myDictionary];
[regions insertObject:region atIndex:0];
NSLog(@"Count: %lu", [regions count]);
[self startMonitoringRegions:regions];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (CLRegion*)mapDictionaryToRegion:(NSDictionary*)dictionary
{
NSString *title = [dictionary valueForKey:@"title"];

CLLocationDegrees latitude = [[dictionary valueForKey:@"latitude"] doubleValue];
CLLocationDegrees longitude =[[dictionary valueForKey:@"longitude"] doubleValue];
CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);

CLLocationDistance regionRadius = [[dictionary valueForKey:@"radius"] doubleValue];

return [[CLRegion alloc] initCircularRegionWithCenter:centerCoordinate
                                               radius:regionRadius
                                           identifier:title];
}

-(void)startMonitoringRegions:(NSMutableArray *)array {
for (CLRegion *GeoFence in array)
{
    NSLog(@"Started monitoring");
    [locationManager startMonitoringForRegion:GeoFence];
}
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
NSLog(@"Exited Region - %@", region.identifier);
// post a notification
}

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
NSLog(@"Exited Region - %@", region.identifier);
// post a notification
}

@end

Here's my *.GPX file:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<gpx
xmlns="http://www.topografix.com/GPX/1/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
version="1.1" 
creator="gpx-poi.com">
<wpt lat="00.0" lon="00.0">
<time>2015-01-01T14:45:02Z</time>
</wpt>
</gpx>

I've replaced my actual coordinates with 00.0.

Could someone please help me with this and why it does't do anything when I enter my *.GPX area? Also, how can I create a more appropriate identifier?

Thanks! Erik

Erik
  • 2,500
  • 6
  • 28
  • 49

1 Answers1

0

I believe you forgot to set the locationManager delegate to your view controller:

    locationManager.delegate = self;

Put this in your viewDidLoad after creating locationManager.

You should also implement monitoringDidFailForRegion to detect any errors. Calling startMonitoring doesn't guarantee it will actually start. It could fail for various reasons.

Mike Taverne
  • 9,156
  • 2
  • 42
  • 58
  • Works excellent now. Just a few questions, I can't see the didFailToStartMonitoring method? Also, does these methods run if the app were not running - what would happen if the app weren't running and I created a UIAlertView inside the didEnterRegion? – Erik Jan 01 '15 at 22:51
  • Sorry, I was going from memory on that. The method is actually monitoringDidFailForRegion. It's a delegate method. Your app will be started by iOS if it is not already running when you enter the region. However, I can't recall right now whether the didEnterRegion method is fired in this case. – Mike Taverne Jan 02 '15 at 00:18
  • I played around with it, if you close the app without swiping it away in the task manager, it will still NSLog when you enter/leave a region, but won't create any UIAlertViews or similar before the app is opened. I didn't check with closing the app by swiping it as that was a little hard with the simulator - but I guess the result would be the same? I couldn't stop the task while debugging, I will try it on my iPhone. But, how can I check if a user is inside the region with for instance a button? Could I wire a BOOL up to the didEnterRegion or is it a better way? – Erik Jan 02 '15 at 00:42