Madoc
Madoc

Reputation: 1625

Sending location to server with CLLocationManager when iphone is in background

i'm having trouble sending my position when the application lies in the background. I'm using CLLocationManager and startMonitoringSignificantLocationChanges. The posision didUpdateToLocation delegate method is performed once, but not more. I've tried to walk around but no new locations is sent to the server.

I have set the "Required background modes" -> "App registers for location updates" in the info.plist file.

Anyone got an idea on what might be wrong?

Code from where the tracking is started:

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = appDelegate;
[appDelegate setLocationManager:locationManager withDistanceFilter:kCLDistanceFilterNone];
[appDelegate.theLocationManager startMonitoringSignificantLocationChanges];

Code (from CLLocationManagerDelegate):

- (void)setLocationManager:(CLLocationManager*)locationManager withDistanceFilter:(CLLocationDistance)distanceFilter {

    // create a new manager and start checking for sig changes
    self.theLocationManager.delegate = nil;
    [theLocationManager release];

    self.theLocationManager = locationManager;
    self.theLocationManager.delegate = self;
    self.theLocationManager.distanceFilter = distanceFilter;
}


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

        NSDate *newLocationTimestamp = newLocation.timestamp;
        NSDate *oldLocationTimestamp = oldLocation.timestamp;

        int locationUpdateInterval = 15;//15 sec

        if (!([newLocationTimestamp timeIntervalSinceDate:oldLocationTimestamp] < locationUpdateInterval)) {
                //NSLog(@"New Location: %@", newLocation);
                [self updateToLocation:newLocation];
        }

    }

    - (void)updateToLocation:(CLLocation *)newLocation {
        NSLog(@"update location!!");

        NSString *latitude = [NSString stringWithFormat:@"%f", [newLocation coordinate].latitude];
         NSString *longitude = [NSString stringWithFormat:@"%f", [newLocation coordinate].longitude];

        [currentUser updatePositionWithLongitude:longitude andLatitude:latitude]; 
    }

Upvotes: 2

Views: 3049

Answers (2)

eric.mitchell
eric.mitchell

Reputation: 8845

Like Bill Brasky said, the accuracy to which you have set your location manager is likely not registering the distance that you have walked. Try setting your location manager accuracy much higher, just to see if works, then dial it back down to a happy medium between accuracy and battery efficiency. Just for testing, take it all the way up:

[appDelegate.theLocationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];

Then instead of:

[appDelegate.theLocationManager startMonitoringSignificantLocationChanges];

try:

[appDelegate.theLocationManager startUpdatingLocation];

Upvotes: 3

Bill Burgess
Bill Burgess

Reputation: 14154

The -startMonitoringForSignificantLocationChanges is directly tied to cell tower connectivity. You may need to travel miles to get connection to a new tower and trigger a location change event. I know that the region monitoring is a bit more accurate as it uses updates of location from Wifi, cell tower, and even other apps that inquire on location. You will need to figure out how accurate and how often you need your app to be. You may need to actively monitor location in the background (which would be a battery killer for sure). Hope this helps.

Upvotes: 2

Related Questions