我将iBeacon配置为:
NSUUID *proximityUUID = [[NSUUID alloc] initWithUUIDString:kUUID];
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:proximityUUID identifier:kIdentifier];
self.beaconRegion.notifyEntryStateOnDisplay = NO;
self.beaconRegion.notifyOnEntry = YES;
self.beaconRegion.notifyOnExit = YES;
当我的应用关闭并且设备被锁定时,didEnterRegion永远不会被触发:
locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
[self sendLocalNotificationForBeaconRegionHello:(CLBeaconRegion *)region];
}
- (void)sendLocalNotificationForBeaconRegionHello:(CLBeaconRegion *)region
{
UILocalNotification *notification = [UILocalNotification new];
notification.alertBody = [NSString stringWithFormat:@"Welcome - %@", region.identifier];
notification.alertAction = NSLocalizedString(@"View", nil);
notification.soundName = UILocalNotificationDefaultSoundName;
notification.fireDate = nil;
notification.hasAction = false;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
即使应用已关闭且手机已锁定,也会调用didExitRegion。我只在解锁手机时进入某个地区时收到通知。
任何想法可能是什么问题?
谢谢
答案 0 :(得分:0)
我怀疑该方法被调用但是由于某种原因吞下了通知(尽管代码看起来还不错)。尝试添加如下方法以帮助弄清楚发生了什么:
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
if(state == CLRegionStateInside) {
NSLog(@"locationManager didDetermineState INSIDE for %@", region.identifier);
}
else if(state == CLRegionStateOutside) {
NSLog(@"locationManager didDetermineState OUTSIDE for %@", region.identifier);
}
else {
NSLog(@"locationManager didDetermineState OTHER for %@", region.identifier);
}
}
在didExitRegion
中查看您的代码进行比较也很有用,并了解有关如何测试进入/退出条件(包括等待时间)的详细信息。