如果应用程序在区域内启动,则startMonitoringForRegion不会调用didEnterRegion

我有一个问题,如果我在该区域内启动应用程序,我的应用程序将不会触发didEnterRegion事件。 如果我在区域外启动应用程序,然后进入该区域,则会触发。 如果我在区域内启动应用程序,然后离开该区域,然后重新进入该区域,它将触发。

任何关于如何在应用程序打开时如何启动它的建议,如果它在该地区将非常感谢!

我认为你不能这样做。

但是,您可以获取当前位置并检查它是否在您自己指定的区域内。 CLCircularRegion有一个containsCoordinate:方法。

我建议你使用这段代码

 [locationManager requestStateForRegion:region]; 

并使用委托方法didDetermineState:来检查状态是CLRegionStateInside还是CLRegionStateOutside。

第一个结论是didEnterRegion与其名称一致地实现。 🙂

CLLocationManagerDelegate实现类似的东西:

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

来自apple的文档:

在注册授权应用程序后立即开始监控地理区域。 但是,不要期望立即收到事件,因为只有边界交叉才会生成事件。 特别是, 如果用户的位置在注册时已经在区域内,则位置管理器不会自动生成事件。 相反,您的应用必须等待用户跨越区域边界,然后才能生成事件并将其发送给代理。 要检查用户是否已经在区域边界内,请使用CLLocationManager类的requestStateForRegion:方法。

所以我最终这样做了:

 #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]; }