locationManager:didEnterRegion and didExitRegion a

2019-06-05 23:41发布

While testing with beacons (ios devices) i found the listener beacon giving some unexpected behavior. locationManager:didEnterRegion method is not getting called even if a beacon enters a region. But the locationManager:didRangeBeacons:inRegion: is getting called correctly, and detected beacons are shown there.

- (void)startListening{

self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
NSUUID *myProximityUUID = [[NSUUID alloc]
                           initWithUUIDString:IDENTIFIER];
_beaconRegion = [[CLBeaconRegion alloc]
                 initWithProximityUUID:myProximityUUID
                 identifier:kPFTransmitterIdentifier];
_beaconRegion.notifyEntryStateOnDisplay = YES;
_beaconRegion.notifyOnEntry  =YES;
[self.locationManager startMonitoringForRegion:self.beaconRegion];

[self.locationManager requestStateForRegion:self.beaconRegion];
//[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];

}

this is the code i have written.

i need to create a local notification when the listener app enters a particular region even if the app isn't running.

4条回答
叛逆
2楼-- · 2019-06-06 00:23

The didEnterRegion method not get called when using notifyEntryStateOnDisplay before monitoring sometimes.I also have tested this condition on my device.Probably you can test once without notifyEntryStateOnDisplay or notifyOnEntry condition .It will help.

查看更多
狗以群分
3楼-- · 2019-06-06 00:26

Have you registered your app for background location updates in the info.plist file? You need to add a row with the array UIBackgroundModes and add an item location: See reference

查看更多
祖国的老花朵
4楼-- · 2019-06-06 00:29

Here's the troubleshooting procedure I would use:

  1. First get it working in the foreground. Run your app in the foreground, and turn off your iBeacon (if it does not have a switch, then pull out the battery or take it 200 feet away.) Wait 10 seconds (you should get a out of region notification in this time) then turn back on your iBeacon and verify you get an in region notification. If you do not get one, I suspect there is something wrong with your callback definition. If this is the case, please post that code.

  2. Once you have it working in the foreground, try to get it working in the background with the shoulder button. Always get the phone to a known state of not being in the region while being in the foreground (using the procedure above), because it takes a long time to make this happen in the background. Once you know you are out of the region, put your app into the background by turning off your screen. Then turn back on your iBeacon. Since you have _beaconRegion.notifyOnEntry =YES, you should get a callback within one second of when you force your display on by hitting the shoulder or home buttons.

  3. Once you have this working, you can go on to letting the phone detect the presence in the background without hitting the shoulder button. Understand though, that this can take longer than you might expect. See this discussion for more details.

查看更多
Rolldiameter
5楼-- · 2019-06-06 00:29

It's possible nothing is wrong with your code. I've noticed that if you turn your test beacon on when the detector is already inside the beacon region (next to it), you will NOT get any notification that you entered the beacon region. That's most likely because iOS did not detect a boundary crossing. iOS seems to notify when it detects that you have crossed the boundary between "out of region" and "inside region" (in either direction) and only then calls the appropriate delegate methods. To test this, simply turn on your beacon but leave your detector OFF. Then, as indicated by davidgyoung above, walk 200 feet away from your beacon. THEN turn the detector ON and walk toward the beacon. You should get a "didEnterBeaconRegion" notification when you enter the region. Another thing you can try is to implement the "locationManagerDidDetermineStateForRegion" delegate method, which is called whenever the state of a defined monitored region changes, which happens whenever you turn on your detector. If you do that, you should get a callback even if your detector is already inside the beacon region when initiated. But note this does not happen in the background so you still need the other callbacks as well.

查看更多
登录 后发表回答