CLLocationManager requestStateForRegion with "When In Use" location access error code 4

Viewed 1527

I am trying to implement iBeacon ranging for an iOS app.

[locationManager requestAlwaysAuthorization];
CLBeaconRegion * region = [self regionFromUUID:uuid];
[locationManager startMonitoringForRegion:region];

In order to determine if the device is inside or outside of the region:

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

This successfully calls:

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
    if (state == CLRegionStateInside) {
        [locationManager startRangingBeaconsInRegion:(CLBeaconRegion*)region];
    } else {
        [locationManager stopRangingBeaconsInRegion:(CLBeaconRegion*)region];
    }
}

and the app is successfully on its way with locationManager:didRangeBeacons:inRegion:.

The problem I am encountering is using requestWhenInUseAuthorization. After locationManager:didStartMonitoringForRegion: calls [location requestStateForRegion:region], the delegate method locationManager:monitoringDidFailForRegion:withError: returns error code 4: "The operation couldn’t be completed".

Swapping requestStateForRegion with startRangingBeaconsInRegion seems to bypass this error and locationManager:didRangeBeacons:inRegion: is successfully called.

Is this a known issue that [locationManager requestStateForRegion:region]; will cause error code 4 if only kCLAuthorizationStatusAuthorizedWhenInUse is granted?

2 Answers
Related