【问题标题】:Geofencing does not notify when entering a region (iOS)进入区域时地理围栏不通知(iOS)
【发布时间】:2016-04-29 07:06:55
【问题描述】:

我正在尝试编写一个应用程序,在用户进入特定区域时通知用户(我有 20 个区域正在根据用户的当前位置进行更新)。

我在-locationManager: didEnterRegion: 上添加了一个断点,但它从未被调用过,尽管用户的当前位置在该区域内(使用模拟器)。

如果有人能够帮助我发现问题,那真的会对我有所帮助。

谢谢!

我的代码:

remindersViewController.m

-(void)startMonitoringClosestStores
{
    [self stopMonitoringStores];

    if (![CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]]) {
        NSLog(@"Monitoring is not available for CLCircularRegion class");
}

for (Store *currentStore in self.twentyClosestStores) {
    CLCircularRegion *region=[currentStore createCircularRegion];
    [self.locationManager startMonitoringForRegion:region];
    }
}
-(void)stopMonitoringStores
{
    for (Store *currentStore in self.twentyClosestStores) {
        CLCircularRegion *region=[currentStore createCircularRegion];
        [self.locationManager stopMonitoringForRegion:region];
    }
}
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    NSLog (@"Enetred"); //Never called
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    self.userLocation=[locations lastObject];
    [self sortClosestStores];
}

Store.m

-(CLCircularRegion*)createCircularRegion
{
    CLCircularRegion *region=[[CLCircularRegion alloc] initWithCenter:self.geoPoint radius:1000 identifier:self.identifier];

    region.notifyOnEntry=YES;

    return region;
}

AppDelegate.m

-(void)handleRegionEvent:(CLRegion*)region
{
    NSLog(@"Geofence triggered");
}

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    NSLog(@"Entered");
}

【问题讨论】:

  • 是否调用了剩余的方法?你在模拟器中模拟了当前位置吗?
  • 请注意模拟器无法检测到您当前的位置。您必须模拟位置模拟器。
  • @Mr.T 实际上,现在我看到(CLLocationManager 的)另一个委托方法也没有被调用。但是self.locationManager.delegate 设置为self。那么可能是什么问题呢?
  • 你在接口部分实现了委托吗?它应该类似于
  • @Mr.T 是的,我做了(在 .h 文件上)。

标签: ios objective-c cllocationmanager geofencing


【解决方案1】:

但它从未被调用过,尽管用户的当前位置在区域内(使用模拟器)。

  1. 不要使用模拟器。仅使用设备进行测试区域监控。您需要亲自去各个地方进行测试。

  2. 地理围栏不涉及您所在的位置。它涉及您跨越的假想线(圆)。如果用户已经在圈内,则不会有通知;如果您只要求“进入”通知,则用户需要离开圈子并再次进入,才能收到通知。

  3. 这段代码可能有问题:

    -(void)startMonitoringClosestStores
    {
         [self stopMonitoringStores];
    

    基本上你在开始之前就停止了。

【讨论】:

  • 我正在清理所有区域并使用新区域进行更新(因为 CLLocationManager 无法同时监控超过 20 个区域,所以我在 didUpdateLocation 上更新了区域
  • 虽然您必须使用设备进行地理围栏测试,但没有必要“亲自去地方”。您可以在连接到 Xcode 的设备上模拟您的步行。模拟的地理围栏有点“理想”,这在现实世界中不会发生,但对于烟雾测试来说仍然足够。请做好准备,并非所有的围栏交叉口都会在现场测试中遇到进入/退出事件。
  • @AlexPavlov 但委托方法根本没有被调用
  • @Yhper 因为你在它们开始之前就关闭了它们(我的观点 #3)。
  • @matt 这不是问题,甚至没有调用 didUpdateLocation
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多