iOS位置更新在后台模式下不会始终如一

时间:2017-03-29 11:54:34

标签: ios iphone cllocationmanager

要求: 触发位置更新当应用程序处于后台时,每秒后回调一次。

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

当前实施:

$certUser.Usercertificate | ForEach-Object{
    Set-ADUser "ME" -certificate @{Add=[System.Security.Cryptography.X509Certificates.X509Certificate]$_}
}

也为背景位置更新完成了plist配置。

请建议还可以采取哪些措施来解决此问题?

1 个答案:

答案 0 :(得分:0)

当发生重大位置更改时,请尝试以下代码进行背景位置更新:

#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];
}