iBeacon : What is the difference between didEnterR

2019-03-24 17:07发布

I want to post a notification when users enter into a region. However, I am very confused because of same two CLLocationManagerDelegate methods. How should I use the two methods properly?

Some people say "didDetermineState" method is needed to start region observation if the app start in the region.

Thanks,

- (void)locationManager:(CLLocationManager *)manager
         didEnterRegion:(CLRegion *)region
{
    [self sendNotification:@"didEnterRegion"];
}

- (void)locationManager:(CLLocationManager *)manager
      didDetermineState:(CLRegionState)state
              forRegion:(CLRegion *)region
{
    switch (state) {
        case CLRegionStateInside:
         [self sendNotification:@"didEnterRegion"];
            break;
        case CLRegionStateOutside:
            break;
        case CLRegionStateUnknown:
            break;
        default:
            break;
    }
} 

标签: ios ibeacon
1条回答
不美不萌又怎样
2楼-- · 2019-03-24 17:53

Apple's documentation for CLLocationManager states:

The location manager calls this method whenever there is a boundary transition for a region. It calls this method in addition to calling the locationManager:didEnterRegion: and locationManager:didExitRegion: methods. The location manager also calls this method in response to a call to its requestStateForRegion: method, which runs asynchronously.

So didDetermineState should get called whenever didEnterRegion/didExitRegion do. In addition, if you explicitly request the state via requestStateForRegion it will be called.

There's one other behaviour that triggers this method: if you're monitoring a region on which you've enabled the notifyEntryStateOnDisplay property, the method will be called whenever the user wakes their device manually, and they are within the region you are monitoring. From the documentation

When set to YES, the location manager sends beacon notifications when the user turns on the display and the device is already inside the region. These notifications are sent even if your app is not running. In that situation, the system launches your app into the background so that it can handle the notifications. In both situations, the location manager calls the locationManager:didDetermineState:forRegion: method of its delegate object.

查看更多
登录 后发表回答