9

I subclassed a CLRegion to support Polygons via overriding containsCoordinate: to use ray casting logic instead of the original distance crunching logic. The subclass is initialized via the normal method (initCircularRegionWithCenter:radius:identifier:), then CLLocationCoordinate2ds are added as NSValues to a mutable array. These coordinates are used during the ray casting logic.

As soon as I try to use the CLRegion subclass, I am confronted with a ton of errors in my application logic, as well as the following error:

2013-07-18 16:46:44.515 Geofencing[4816:907] (identifier 6C11CBAF-3EE4-4257-9D75-9724F4349B5D) <+39.86605072,-75.54420471> radius 186.54m: Error Domain=kCLErrorDomain Code=5 "The operation couldn’t be completed. (kCLErrorDomain error 5.)"

I also tried a different subclass that does not modify any methods but adds a method for reading metadata from an NSDictionary. I was confronted with the same error.

What is going on? Is subclassing CLRegion feasible?

Nate Symer
  • 2,185
  • 1
  • 20
  • 27
  • Possible duplicate of [iOS 7 CoreLocation: region monitoring fails on the first time after location services are authorised](https://stackoverflow.com/questions/22404620/ios-7-corelocation-region-monitoring-fails-on-the-first-time-after-location-ser) – Senseful Aug 21 '18 at 04:58

7 Answers7

20

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.

Nate Symer
  • 2,185
  • 1
  • 20
  • 27
  • 6
    See other answers; error code 5 does not denote this, it appears to be a 'catch all' code for various error conditions. – Carlos P Sep 01 '14 at 20:07
  • does not *only* denote this. You do get this message if you try to add > 20 beacons. – Ian Dundas Oct 29 '14 at 11:28
  • Happens to me as well, adding 25 beacons returns 5 aforementioned error codes. – user1872384 Apr 06 '15 at 01:44
  • what is the solution to remove monitor region? I tried with stopMonitoringForRegion which i registered, but didn't work for. – Nirmalsinh Rathod Sep 13 '16 at 05:28
  • 1
    you can get this while monitoring 1 region, this shouldn't be the accepted answer to ```CoreLocation kCLErrorDomain error 5```, even if it was the answer for the OP, perhaps update the question so other people don't end up here – Saik Caskey Sep 01 '17 at 15:24
  • Can you tell how can we define if `CLBeaconRegion` is nil ? – Jack Mar 21 '18 at 10:08
  • Maybe it is because you forgot to turn on your Bluetooth – FBC Jan 31 '19 at 12:02
  • I can confirm that more than 20 regions trigger this issue, and is actually when the uuuid (last hex unit) is more than 19 (UInt8(19)) – Cristian Zumelzu Jan 28 '22 at 15:33
11

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];
    }
}
Nate Symer
  • 2,185
  • 1
  • 20
  • 27
user3648465
  • 111
  • 1
  • 3
  • what exactly happened to me. Would be nice if solution is just showing up alert. – PH7 Jul 03 '14 at 00:39
  • 2
    After googling around, I found this http://www.cocoanetics.com/2014/05/radar-monitoring-clregion-immediately-after-removing-one-fails/ .. It works.. Yayy!!. – PH7 Jul 03 '14 at 01:49
  • What can I do to avoid this error in the same scenario? I need to stop monitoring a region and start monitoring another region at the same time. – PGDev Jul 01 '16 at 10:27
7

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

wspruijt
  • 1,037
  • 11
  • 15
3

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.)

Sean McMains
  • 57,907
  • 13
  • 47
  • 54
2

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

Shmidt
  • 16,436
  • 18
  • 88
  • 136
0

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.

iRiziya
  • 3,235
  • 1
  • 22
  • 36
-1

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

FBC
  • 1,047
  • 8
  • 18