Ios 来自didEnterRegion的多个本地通知故障:
我目前正在监视几个由核心数据支持的位置。 换句话说,我已经建立了一个for循环,该循环遍历核心数据中所有存储的实体,并为所有实体创建一个监控区域 这里的问题是for循环在进入其中一个区域时会触发多个本地通知。通知的数量几乎直接对应于受监控区域的数量。所以我很有信心这可能是导致这个错误的原因,但我不是100%确定 我注意到这似乎是区域监视的常见问题,但我还没有找到一个包含for循环的示例 如何在调用didEnterRegion时停止触发多个通知 在viewDidLoad中调用以下方法。[DataSource sharedInstance].fetchedResultItems是一个数组,该数组由已获取请求中的已获取对象填充Ios 来自didEnterRegion的多个本地通知故障:,ios,localnotification,region-monitoring,Ios,Localnotification,Region Monitoring,我目前正在监视几个由核心数据支持的位置。 换句话说,我已经建立了一个for循环,该循环遍历核心数据中所有存储的实体,并为所有实体创建一个监控区域 这里的问题是for循环在进入其中一个区域时会触发多个本地通知。通知的数量几乎直接对应于受监控区域的数量。所以我很有信心这可能是导致这个错误的原因,但我不是100%确定 我注意到这似乎是区域监视的常见问题,但我还没有找到一个包含for循环的示例 如何在调用didEnterRegion时停止触发多个通知 在viewDidLoad中调用以下方法。[DataS
-(void)startMonitoringRegions{
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
CLAuthorizationStatus authorizationStatus = [CLLocationManager authorizationStatus];
if (authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {
self.locationManager.distanceFilter = 10;
[self.locationManager startUpdatingLocation];
for (POI *items in [DataSource sharedInstance].fetchResultItems){
NSString *poiName = items.name;
NSNumber *poiLatitude = items.yCoordinate;
NSLog(@"value: %@", poiLatitude);
NSNumber *poiLongitude = items.xCoordinate;
NSLog(@"value: %@", poiLongitude);
NSString *identifier = poiName;
CLLocationDegrees latitude = [poiLatitude floatValue];
CLLocationDegrees longitude = [poiLongitude floatValue];
CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);
self.regionRadius = 10;
self.region = [[CLCircularRegion alloc] initWithCenter:centerCoordinate radius:400 identifier:identifier];
[self.locationManager startMonitoringForRegion:self.region];
NSLog(@"region: %@", self.region);
NSLog(@"monitored regions %@", self.locationManager.monitoredRegions);
}
}
}
}
这里是didEnterRegion方法
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
NSLog(@"entered region!!");
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification) {
localNotification.fireDate = nil;
localNotification.alertBody = [NSString stringWithFormat:@"You are near %@", self.region.identifier];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
}
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// [[UIApplication sharedApplication]presentLocalNotificationNow:localNotification];
}
区域作为共享资源发挥作用。当您进入任何区域时,呼叫将转发给所有location manager。我认为您正在某处创建多个location manager对象。这实际上导致了didEnterRegion的多次调用。调用didEnterRegion的时间取决于您注册的LocationManager的数量。您应该使用此方法在AppDelegate中编写代码
- (BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{
}只是一个故障排除提示。您可以使用以下等效的Obj-C来查看应用程序当前正在监视哪些区域。也许审查标识符会对这个问题有所帮助
for region in locationManager.monitoredRegions {
debugPrint(region.identifier)
}
为了一个干净的开始,您可以使用以下命令删除所有区域:
for region in locationManager.monitoredRegions {
locationManager.stopMonitoringForRegion(region)
}
我假设debugPrint=NSLog。我只知道目标c。很抱歉没有把它翻译成Obj-C,我只是从我的项目中复制并粘贴了它,希望它的意思会相当清楚。