iphone - 为什么 didEnterRegion 调用了两次?

标签 iphone ios cllocationmanager clregion

我在我的 iOS 应用程序中使用位置服务,即区域监控, 这是我的代码

//this for creating region
-(void)createRegion
{
    [dictionary setValue:@"23 St, New York" forKey:@"title"];
    [dictionary setValue:[NSNumber numberWithDouble:40.742878] forKey:@"latitude"];
    [dictionary setValue:[NSNumber numberWithDouble:-73.992821] forKey:@"longitude"];
    [dictionary setValue:[NSNumber numberWithDouble:(300.0)] forKey:@"radius"];

    [regionArray addObject:[self mapDictionaryToRegion:dictionary]];
    [self initializeRegionMonitoring:regionArray];
}
- (CLRegion*)mapDictionaryToRegion:(NSDictionary*)dictionary {
    NSString *title = [dictionary valueForKey:@"title"];

    CLLocationDegrees latitude = [[dictionary valueForKey:@"latitude"] doubleValue];
    CLLocationDegrees longitude =[[dictionary valueForKey:@"longitude"] doubleValue];
    CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);

    CLLocationDistance regionRadius = [[dictionary valueForKey:@"radius"] doubleValue];

    return [[CLRegion alloc] initCircularRegionWithCenter:centerCoordinate
                                                   radius:regionRadius
                                               identifier:title];
}

- (void) initializeRegionMonitoring:(NSArray*)geofences {
    if(![CLLocationManager regionMonitoringAvailable]) {
       // [self showAlertWithMessage:@"This app requires region monitoring features which are unavailable on this device."];
        return;
    }

    for(CLRegion *geofence in geofences) {
        [_locationManager startMonitoringForRegion:geofence];
    }
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(@"entered region %@",region.identifier);
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    NSLog(@"exited region %@",region.identifier);
}

当应用程序处于前台时它工作正常。它向我显示日志:“进入区域..”和“退出区域..”, 但是当应用程序进入后台时,相同的日志会在几分之一秒内打印两次,即委托(delegate)方法调用两次,我不需要,有什么办法可以避免调用 2 次吗? 还是在创建或监控区域时犯了错误? 请帮我 .. 提前致谢..

最佳答案

我相信这个问题与我自己的问题重复。我认为 Apple 的区域监控 API 存在错误。我已经就此问题向 Apple 提交了一份雷达报告,但到目前为止还没有收到任何回复。

我解决这个问题的一种方法是将时间戳保存在 didEnter 和 didExit 方法中,如果这些方法在保存的时间戳后 10 秒内触发,只需跳过该方法作为欺骗。

如果有人感兴趣,我在 github 上有一个项目展示了这个问题。

https://github.com/billburgess/GeofenceBug

请随时提交另一个雷达,因为这是 Apple 注意到问题并采取行动的唯一方式。雷达编号为 12452255 - 重复代表调用区域监控。

如果你想欺骗这个雷达,这里是开放的雷达链接和信息。 http://openradar.appspot.com/radar?id=2484401

关于iphone - 为什么 didEnterRegion 调用了两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14054235/

相关文章:

ios - 如何从 UIImagePickerController 选择多个图像

ios - 您可以将 AutoLayout 与 UISplitViewController 结合使用吗?

iphone - iOS - 为什么我的 NSURL 没有保留在 init 方法中?

ios - CLLocationManager 在设备上不工作

ios - 如何在 Swift 中使用 MapKit 将叠加层粘贴到用户位置

iphone - 需要访问tableView中的单元格:heightForRowAtIndexPath:

ios - 我们如何将我们在 dribbble 上看到的精彩动画实现到 ios 应用程序?

ios - 为什么 SKSpriteNode.addChild() 不工作

ios - ranlib 缺少在 iOS 上编译 GNU bash

iphone - 如何从当前位置获取 Google map 链接?