【问题标题】:Delegate methods of CLLocationManager didEnterRegion, didExitRegion and didDetermineState are not being called on iOS 8.4 (device)CLLocationManager didEnterRegion、didExitRegion 和 didDetermineState 的委托方法未在 iOS 8.4(设备)上调用
【发布时间】:2015-10-01 14:34:18
【问题描述】:

我正在为 Goefencing 做演示。

这是我的代码,

- (void) registerRegionForMonitoring {

    if (self.locationManager == nil) {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.distanceFilter = kCLDistanceFilterNone;

        if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [self.locationManager requestAlwaysAuthorization];
        }

        [self.locationManager startUpdatingLocation];
    }

    if(![CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]]) {
        [Utilities showAlertWithTitle:@"Geofence" andMessage:@"This app requires region monitoring features which are unavailable on this device."];
        return;
    }

    for(NSDictionary *dictionary in geofences) {

        NSString *geofenceId = [NSString stringWithFormat:@"%@", [dictionary valueForKey:@"geofence_id"]];
        CLLocationDegrees latitude = [[dictionary valueForKey:@"latitude"] doubleValue];
        CLLocationDegrees longitude =[[dictionary valueForKey:@"longitude"] doubleValue];
        CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);
        CLLocationDistance regionRadius = [[dictionary valueForKey:@"radius"] doubleValue];

        CLRegion *geofenceRegion = [[CLCircularRegion alloc] initWithCenter:centerCoordinate radius:regionRadius identifier:geofenceId];
        [self.locationManager startMonitoringForRegion:geofenceRegion];
   }
}

这里是委托方法

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

    CLLocation *latestLocation = [locations lastObject];
    self.currentLocation = latestLocation;
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    [Utilities showAlertWithTitle:@"Geofence" andMessage:[NSString stringWithFormat:@"You are in region with identifire %@", region.identifire]];
}

- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
    [self.locationManager requestStateForRegion:region];
}

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    [Utilities showAlertWithTitle:@"Geofence" andMessage:[NSString stringWithFormat:@"You are out of region with identifire %@", region.identifire]];
}

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {

    if (state == CLRegionStateInside) {
        //  if user already in the region, when monitoring is started
        [Utilities showAlertWithTitle:@"Geofence" andMessage:[NSString stringWithFormat:@"You are in region with identifire %@", region.identifire]];
    }
}

我还在 iOS 8+ 的 plist 文件中添加了密钥 "NSLocationAlwaysUsageDescription"

下面还添加了背景模式:

  • 应用注册位置更新

  • 应用使用 CoreBluetooth 共享数据

  • 应用使用 CoreBluetooth 进行通信

  • 应用下载内容以响应推送通知

在 iOS 7 上一切正常,在 iOS 8.4 的 iOS 模拟器中也能正常工作

无法在设备上运行(iOS 8.4,iPad 和 iPod)

我已经检查了 wifi 是否打开。

我还在设置中打开了“常规”->“后台应用刷新”

我不知道我错过了什么?

欢迎所有建议。提前致谢。

【问题讨论】:

  • 您能解释一下在设备上运行时您是如何测试应用程序的吗?它是通过 Xcode 模拟的跑腿工作还是位置(提示:结果可能不同)
  • @AlexPavlov :感谢您的即时回复。我所做的是,我添加了半径为 1000m 的当前位置坐标来创建区域。然后添加该区域进行监控。所以,它应该调用determineState 方法。但它没有。虽然同样的事情在我的 iOS 模拟器 8.4 上工作

标签: ios objective-c cllocationmanager geofencing ios8.4


【解决方案1】:

您是否尝试过检查该区域是否真的开始跟踪?该方法会返回错误。

- (void)locationManager:monitoringDidFailForRegion:withError:

如果您在从 GPS 获得良好位置之前调用 start monitoringForRegion,您可能会收到错误消息。

此外,您可能需要检查didDetermineState 中的状态CLRegionStateUnknown,因为这也是一个选项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 2016-05-07
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 2013-12-03
    • 2011-03-12
    相关资源
    最近更新 更多