3

我有一个应用程序可以捕获用户位置并将此信息发送到我的服务器。

我已经启动了 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 中调用此方法 - (void)applicationDidEnterBackground:(UIApplication *)application { [locationManager startMonitoringSignificantLocationChanges]; }

当应用程序返回前台时:

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

但我有一些疑问。这样,如果应用程序处于关闭状态,我的应用程序会继续与服务器通信位置吗?

仅当我处于后台或前台状态时如何发送位置?

编辑:我还设置了:

UIBackgroundModes 位置

4

1 回答 1

2

不需要此代码,这将在后台激活您的应用程序,您几乎不必在项目的 info.plist 中提及所需的后台模式,并且该值应该是 App registers for location updates。

仅在 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];
       }];
*/
}
于 2013-04-30T10:29:28.950 回答