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]; }
onmir source share