StartMonitoringForRegion does not call didEnterRegion if the application is running in a region

I have a problem when my application will not fire the didEnterRegion event if I run the application in the region. If I run the application outside the region and then enter the region, it starts. If I launched the application inside the region, then leave the region, and then enter the region again, it works.

Any suggestions on how to run it, as soon as the application opens, if it is in this region, I will be very grateful!

+4
source share
4 answers

I do not think you can do this.

But you can get the current location and check if it is inside the region that you specified yourself. CLCircularRegion has a containsCoordinate: method for this.

+3
source

I suggest you use this code

 [locationManager requestStateForRegion:region]; 

And use the delegate method didDetermineState: to check if the state is CLRegionStateInside or CLRegionStateOutside.

+6
source

The first conclusion is that didEnterRegion is implemented sequentially with its name. :)

Add something like this to CLLocationManagerDelegate :

 - (void) locationManager: (CLLocationManager *) manager didStartMonitoringForRegion: (CLRegion *) region { if ([self insideRegion: region location: manager.location]) [self locationManager: manager didEnterRegion: region]; } 
+2
source

From the Apple documentation:

Geographic region monitoring begins immediately after registration for authorized applications. However, do not expect to receive the event immediately, because only border crossings generate the event. In particular, if the user's location is already inside the region during registration, the location manager will not automatically generate an event. Instead, your application should wait for the user to move the border of the area before the event occurs and is sent to delegate. To check if the user is already within the regionโ€™s border, use the requestStateForRegion: method of the CLLocationManager class.

So I finished this:

 #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) NSDictionary *regionDictionary; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // setup regions in case you have multiple regions self.regionDictionary = @{@"com.test" : @"2FAE2A83-1634-443B-8A0C-56704F81A181"}; // setup location manager self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { [self.locationManager requestAlwaysAuthorization]; } [self.locationManager startUpdatingLocation]; //start monitoring for all regions for (NSString *key in self.regionDictionary.allKeys) { CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:self.regionDictionary[key]] identifier:key]; [self.locationManager startMonitoringForRegion:beaconRegion]; } } - (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region { if (region.identifier.length != 0) { CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:self.regionDictionary[region.identifier]] identifier:region.identifier]; [self.locationManager startRangingBeaconsInRegion:beaconRegion]; } } - (void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion *)region { if (region.identifier.length != 0) { CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:self.regionDictionary[region.identifier]] identifier:region.identifier]; [self.locationManager stopRangingBeaconsInRegion:beaconRegion]; } } - (void)locationManager:(CLLocationManager*)manager didRangeBeacons:(NSArray*)beacons inRegion:(CLBeaconRegion*)region { // Beacon found! CLBeacon *foundBeacon = [beacons firstObject]; NSLog(@"UUID:%@; major:%@; minor:%@;", foundBeacon.proximityUUID.UUIDString, foundBeacon.major, foundBeacon.minor); } - (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region { if ([region isKindOfClass:[CLBeaconRegion class]] && state == CLRegionStateInside) { [self locationManager:manager didEnterRegion:region]; } } - (void)locationManager:(CLLocationManager *) manager didStartMonitoringForRegion:(CLRegion *) region { [manager requestStateForRegion:region]; } 
+1
source

Source: https://habr.com/ru/post/1498075/


All Articles