【问题标题】:iOS Location Updates not trigerred consistently in background modeiOS 位置更新不会在后台模式下不断触发
【发布时间】:2017-03-29 11:54:34
【问题描述】:

要求: 应用在后台时,每秒触发一次位置更新回调。

问题: 位置回调不会在每一秒后触发。相反,我们有时会在 1 秒后不一致地得到它们,有时会在 4 秒后甚至有 40-50 秒的间隔。

当前实施:

setActivityType = CLActivityTypeOther
setAllowsBackgroundLocationUpdates = YES
setDesiredAccuracy = kCLLocationAccuracyBestForNavigation
setDistanceFilter = kCLDistanceFilterNone
setPausesLocationUpdatesAutomatically = false

plist 配置也用于后台位置更新。

请建议可以做些什么来解决这个问题?

【问题讨论】:

  • 您使用哪个选项来更新位置。是startMonitoringSignificantLocationChanges 还是startUpdatingLocation
  • 顺便问一下,你在Background ModesCapabilites 部分打开了Location updates 吗?
  • @Poles:我正在使用 startUpdatingLocation 方法,是的,我在 Capabilities 部分打开了 Location 标志。

标签: ios iphone cllocationmanager


【解决方案1】:

在发生重大位置更改时尝试以下代码以进行后台位置更新:

#pragma mark - CLLocationManager

- (void)startContinuosLocationUpdate
{
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

    if (status == kCLAuthorizationStatusDenied)
    {
        NSLog(@"Location services are disabled in settings.");
    }
    else
    {
        // for iOS 8
        if ([self.anotherLocationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
        {
            [self.anotherLocationManager requestAlwaysAuthorization];
        }
        // for iOS 9
        if ([self.anotherLocationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)])
        {
            [self.anotherLocationManager setAllowsBackgroundLocationUpdates:YES];
        }

        [self.anotherLocationManager startUpdatingLocation];
    }
}

- (void)stopContinuosLocationUpdate
{
    [self.anotherLocationManager stopUpdatingLocation];
}

- (void)startMonitoringLocation
{
    if (_anotherLocationManager)
        [_anotherLocationManager stopMonitoringSignificantLocationChanges];

    self.anotherLocationManager = [[CLLocationManager alloc]init];
    _anotherLocationManager.delegate = self;
    _anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    _anotherLocationManager.activityType = CLActivityTypeOtherNavigation;

    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")) {
        [_anotherLocationManager setAllowsBackgroundLocationUpdates:YES];
    }
    else if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
        [_anotherLocationManager requestAlwaysAuthorization];
    }
    [_anotherLocationManager startMonitoringSignificantLocationChanges];
}

- (void)restartMonitoringLocation
{
    [_anotherLocationManager stopMonitoringSignificantLocationChanges];

    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")) {
        [_anotherLocationManager setAllowsBackgroundLocationUpdates:YES];
    }
    else if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
        [_anotherLocationManager requestAlwaysAuthorization];
    }
    [_anotherLocationManager startMonitoringSignificantLocationChanges];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多