[iOS的]捕捉用户的位置:澄清([iOS]capturing user location: cla

2019-09-01 10:58发布

我有一个应用程序,捕捉用户位置和发送此信息到我的服务器。

我已经开始用的LocationManager服务:

- (CLLocationManager *)locationManager {
    if (_locationManager == nil) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        isUpdatingLocation = NO;
    }
    return _locationManager; 
}

-(void) startLookingForLocation{

    [self locationManager].desiredAccuracy = kCLLocationAccuracyBest;
    [self locationManager].distanceFilter = 250;

    if([[self locationManager]respondsToSelector:@selector(setPausesLocationUpdatesAutomatically:)])
        [[self locationManager] setPausesLocationUpdatesAutomatically:YES];

    if([[self locationManager]respondsToSelector:@selector(setActivityType:) ])
        [[self locationManager] setActivityType:CLActivityTypeFitness];

    [[self locationManager] startUpdatingLocation];
    [[self locationManager] startUpdatingHeading];

    isUpdatingLocation = YES;
}

与停止它:

-(void) stopLookingForLocation{

    [[self locationManager] stopUpdatingLocation];
    [[self locationManager] stopUpdatingHeading];

    isUpdatingLocation = NO;

}

所以,当我检测用户位置的变化:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    [self setCurrentLocation:newLocation];

    BOOL isInBackground = NO;
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
    {
        isInBackground = YES;
    }

    if (isInBackground)
    {
        UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication]
                  beginBackgroundTaskWithExpirationHandler:
                  ^{
                      [[UIApplication sharedApplication] endBackgroundTask:bgTask];
                  }];

        // Send user-location to my server

        if (bgTask != UIBackgroundTaskInvalid)
        {
            [[UIApplication sharedApplication] endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;
        }
    }
    else
    {
        // Send user-location to my server
    }

}

和所有工作正常。 (我想)。

当应用程序进入后台模式,我调用内的appDelegate此方法 - (无效)applicationDidEnterBackground:(UIApplication的*)应用{[的LocationManager startMonitoringSignificantLocationChanges]; }

当应用程序返回前台:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [locationManager stopMonitoringSignificantLocationChanges];
}

但是我有些疑惑。 这样一来,如果应用程序处于关闭状态,我的应用程序继续comunicate位置服务器?

如何只有当我在后台或前台状态,是我可以发送位置?

编辑:我还设置:

UIBackgroundModes位置

Answer 1:

有没有必要这个代码这将活跃你的背景,你就不得不提到所需的背景模式,在项目的info.plist和值应为应用程序注册了位置更新时间很少应用。

只有在info.plist中设置UIBackgroundModes位置和它的作品在后台这可能会帮助你。

//In app deligate 

#pragma mark -
#pragma mark  GPS Service

//========== start GPS SERVICE ===========
-(void)startGpsService
{

    locationManager=[[CLLocationManager alloc] init];
    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    locationManager.distanceFilter=10.0;
    [locationManager startUpdatingLocation];
    [self performSelectorInBackground:@selector(sendDataToServer) withObject:nil];
}

#pragma mark -
#pragma mark CLLocation Manager Delegates

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation
{
      [self performSelectorInBackground:@selector(sendDataToServer) withObject:nil];

}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // No need to do any thing 
   /* UIApplication *app = [UIApplication sharedApplication];
      UIBackgroundTaskIdentifier bgTask = 0;
      bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
      [app endBackgroundTask:bgTask];
       }];
*/
}


文章来源: [iOS]capturing user location: clarification