startMonitoringForRegion is not calling didEnterRegion if the app is started within the region

大城市里の小女人 提交于 2019-12-10 17:37:26

问题


I'm having an issue where my app will not fire the didEnterRegion event if I start the app within the region. If I start the app outside the region and then enter the region, it fires. If I start the app inside the region, then leave the region, then re-enter the region, it fires.

Any suggestions on how to get it to fire as soon as the app is opened if it's in the region would be much appreciated!


回答1:


I don't think you can do that.

But, you can get the current location and check if it's inside the region you're specifying yourself. CLCircularRegion has a containsCoordinate: method for this.




回答2:


I suggest you to use this code

[locationManager requestStateForRegion:region];

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




回答3:


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

Implement something like this in your CLLocationManagerDelegate:

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



回答4:


From apple's documentation:

Monitoring of a geographical region begins immediately after registration for authorized apps. However, don’t expect to receive an event right away, because only boundary crossings generate an event. In particular, if the user’s location is already inside the region at registration time, the location manager doesn’t automatically generate an event. Instead, your app must wait for the user to cross the region boundary before an event is generated and sent to the delegate. To check whether the user is already inside the boundary of a region, use the requestStateForRegion: method of the CLLocationManager class.

So I ended up doing 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];
}


来源:https://stackoverflow.com/questions/18343960/startmonitoringforregion-is-not-calling-didenterregion-if-the-app-is-started-wit

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