3

I've an app that capture user location and send this info to my server.

I've started the LocationManager service with:

- (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;
}

and stopping it with:

-(void) stopLookingForLocation{

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

    isUpdatingLocation = NO;

}

So, when I detect a change of user location:

- (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
    }

}

and all works correctly. (I suppose).

When app go into background mode, I invoke this method within appDelegate - (void)applicationDidEnterBackground:(UIApplication *)application { [locationManager startMonitoringSignificantLocationChanges]; }

and when app return to foreground:

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

But I have some doubts. In this way, if app is in closed status, my app continues to comunicate location to server?

How I can send location only when I'm in background or foreground status?

EDIT: I've also set:

UIBackgroundModes location

CeccoCQ
  • 3,746
  • 12
  • 50
  • 81

1 Answers1

2

There is no need of this code this will active your application in background for Little time you have to mention Required background modes in info.plist of the project and the value should be App registers for location updates.

only Set UIBackgroundModes location in info.plist and it works in background this may help you.

//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];
       }];
*/
}
Divyam shukla
  • 2,038
  • 14
  • 18
  • @Cecco I have already faced this problem and then finally got this solution. – Divyam shukla Apr 30 '13 at 10:35
  • Sorry, but I'm a newbie of Objective-C. Can you post me a snippet of your code so I can understand this question? Thanks for your response. – CeccoCQ Apr 30 '13 at 10:36
  • Thanks a lot. But this snippet send the location to server only when app is in foreground, or also when is in background or closed? I suppose only in background and foreground state (for closed I mean when user make a double tap on home key and close the app). – CeccoCQ Apr 30 '13 at 10:53
  • @Cecco this will also work in background but if app is closed then we cannot do anything – Divyam shukla Apr 30 '13 at 10:55
  • It's right and this is all I need. But the GPS icon (at top right corner, near the battery icon) is still visible when I close the app. So I can think that my device are sending my current location. Any ideas? – CeccoCQ Apr 30 '13 at 10:58
  • either that location service may be used by any other app like maps. But if only your app will use GPS then this will automatically shutdown after sometime – Divyam shukla Apr 30 '13 at 11:00