我想在用户进入某个区域时发布通知。但是,由于相同的两个 CLLocationManagerDelegate 方法,我很困惑。我应该如何正确使用这两种方法?
有人说,如果应用程序在区域中启动,则需要“didDetermineState”方法来启动区域观察。
谢谢,
- (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;
}
}
最佳答案
Apple 的 documentation for CLLocationManager状态:
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:
andlocationManager:didExitRegion:
methods. The location manager also calls this method in response to a call to itsrequestStateForRegion:
method, which runs asynchronously.
所以 didDetermineState
应该在 didEnterRegion
/didExitRegion
执行时被调用。此外,如果您通过 requestStateForRegion
明确请求状态,它将被调用。
还有一种其他行为会触发此方法:如果您正在监视已启用 notifyEntryStateOnDisplay
属性的区域,则只要用户手动唤醒他们的设备,就会调用该方法,并且他们在您正在监视的区域内。来自 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.
关于ios - iBeacon : What is the difference between didEnterRegion and didDetermineState(CLRegionStateInside),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21214412/