Get StartMonitoringForRegion Working

iBeacon StartMonitoringForRegion Doesn’t Work

knowventBanner

So I was scratching my head trying to the the iBeacon monitoring tooling to return a notification on my phone. I thought there was a problem with my app’s implementation of the Core Location’s Location Manager.

All I wanted was to have the - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region or - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region to be called. Read on to see what things I tried.

A Couple Things I Checked for StartMonitoringForRegion

Made Sure the App Has Right Background Modes Set

I made sure that Location updates and Acts as a Bluetooth LE Accessories was enabled. This is required for StartMonitoringForRegion to work.

iOS Background Modes

But it still StartMonitoringForRegion didn’t work..


Location permissions were granted

Just like push notifications, current location, access to contact you need to ask permission from the use. To make sure I clicked allow instead of deny I implemented the following delegate method:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"Couldn't turn on ranging: Location services are not enabled.");
    }

    if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorized) {
        NSLog(@"Couldn't turn on monitoring: Location services not authorised.");
    }
}

The Location Manger was successfully gained access. So, I haven’t resolved my problem of getting didEnterRegion or didExitRegion to be called.


An Error with Monitoring?

The monitoringDidFailForRegion delegate method would receive errors related to region monitoring. I made sure I implemented this method:

- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
    NSLog(@"monitoringDidFailForRegion - error: %@", [error localizedDescription]);
}

From the Apple Docs: If an error occurs while trying to monitor a given region, the location manager sends this message to its delegate. Region monitoring might fail because the region itself cannot be monitored or because there was a more general failure in configuring the region monitoring service.

Still not luck. I didn’t receive any error related to monitoring for the specified beacon region. So, what gives?


It Turns Out… StartMonitoringForRegion Not Working Had Nothing to Do With My Code

After doing some research I found out that my phone’s Background App Refresh needed to be enabled. I disabled this feature because I was told it would help preserve my battery life. Turns out that disabling Background App Refresh also keeps stops iBeacon monitoring.

App Background Refresh

If you want to check this in your app to notify the user or handle this situation you can use the following snippet. Note the UIBackgroundRefreshStatusDenied enum. This was the setting that was returned when I had App Background Refresh disabled.

-(BOOL)CanDeviceSupportAppBackgroundRefresh
{
    // Override point for customization after application launch.
    if ([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusAvailable) {
        NSLog(@"Background updates are available for the app.");
        return YES;
    }else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusDenied)
    {
        NSLog(@"The user explicitly disabled background behavior for this app or for the whole system.");
        return NO;
    }else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusRestricted)
    {
        NSLog(@"Background updates are unavailable and the user cannot enable them again. For example, this status can occur when parental controls are in effect for the current user.");
        return NO;      
    }
}

So, Try all of the thing listed above. Ultimately, nothing with StartMonitoringForRegion if App Background Refresh is disabled.

Let us know if this helped you.

Posted in iOS, Mobile, Programming and tagged , , , .
  • Pingback: iOS iBeacon Tutorial | CLE Developer - Justin Hyland()

  • Saleem

    Thanks Man! i wasted my whole day and just found out that my background app refresh was off..

    • Justin Hyland

      @disqus_aqOZ1lKaxC:disqus That is the same reason it compelled me to write this article. I am glad it helped you out.