CoreLocation kCLErrorDomain error 5

蓝咒 提交于 2019-12-03 00:14:07

I hate to answer my own question, but I have found the solution to my issue. a kCLErrorDomain code/error of 5 denotes that you have tried to monitor more than 20 CLRegions. In my case, both subclasses were guilty of monitoring more than 20 regions.

user3648465

It also happens if you:

stop monitoring a region

[self.manager stopMonitoringForRegion:region];

and request the state for all monitored regions shortly afterwards:

for (CLRegion *region in self.manager.monitoredRegions) {
    [self.manager requestStateForRegion:region];
}

you will get the kCLErrorDomain 5 because IOS seems to have disabled the monitoring for that region, but has not yet removed it from the monitoredRegions array

monitoringDidFailForRegion CLCircularRegion (identifier:'Home', center:<...>, radius:102.00m) The operation couldn’t be completed. (kCLErrorDomain error 5.)
monitoredRegion: CLCircularRegion (identifier:'Home', center:<...>, radius:102.00m)
monitoredRegion: CLBeaconRegion (identifier:'BeaconHome', uuid:<..., major:(null), minor:(null))
monitoredRegion: CLCircularRegion (identifier:'D...', center:<...>, radius:101.00m)
monitoredRegion: CLCircularRegion (identifier:'W...', center:<..>, radius:51.00m)

to work around that problem, do something like this:

- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
    NSLog(@"monitoringDidFailForRegion %@ %@",region, error.localizedDescription);
    for (CLRegion *monitoredRegion in manager.monitoredRegions) {
        NSLog(@"monitoredRegion: %@", monitoredRegion);
    }
    if ((error.domain != kCLErrorDomain || error.code != 5) &&
        [manager.monitoredRegions containsObject:region]) {
        NSString *message = [NSString stringWithFormat:@"%@ %@",
            region, error.localizedDescription];
        [AlertView alert:@"monitoringDidFailForRegion" message:message];
    }
}

Also: if you're testing with iBeacons, you can't use the iOS simulator.

It is also possible to get this error code back when your latitude and longitude values don't make sense. (I'd transposed them, for example, and was vexed by this error for a while.)

This error could also rise up if added CLRegion is nil.

If anybody is still struggling with this then take a look here:

In my case, I had to call requestAlwaysAuthorization just before calling startMonitoring and it worked like charm!

locationManager.requestAlwaysAuthorization()

let currRegion = CLCircularRegion(center: CLLocationCoordinate2D(latitude: LAT, longitude: LONG, radius: 100, identifier: "MyLocation")
currRegion.notifyOnEntry = true

locationManager.startMonitoring(for: region)

Btw, I would love to thank https://shrikar.com/swift-tutorial-corelocation-and-region-monitoring-in-ios-8/ for this.

I got this error because i did not start the Bluetooth. So... do not forget to start your Bluetooth ;)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!