2

I started developing a project using ios-5 , i want to find current location in every 15 min.

this is my code

- (void)applicationDidEnterBackground:(UIApplication *)application {

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSDate *newLocationTimestamp = newLocation.timestamp;
    NSDate *lastLocationUpdateTiemstamp;
    int locationUpdateInterval = 60; //1min

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if (userDefaults) {
        lastLocationUpdateTiemstamp = [userDefaults objectForKey:@"myevent"];
     if (!([newLocationTimestamp timeIntervalSinceDate:lastLocationUpdateTiemstamp] < locationUpdateInterval)) {
            NSLog(@"New Location: %@", newLocation);

            //[self locationManager:locationManager didUpdateToLocation:newLocation fromLocation:oldLocation];
            [userDefaults setObject:newLocationTimestamp forKey:@"myevent"];
        }
    }
}

The problem I am having is that this code is working in background but i got current location(here is newLocation) in every 5min not after 1min, even i set locationUpdateInterval = 60sec

I have tried everything but not getting exact solution. Thanks in advance!

akshata
  • 41
  • 3

1 Answers1

1

The anecdotal evidence I've heard is that, in order to save battery life, CoreLocation updates are only fired periodically for an application that lives in the background.

There are a few related questions I've seen here regarding this subject, like this one.

Also, take a look at Apple's Location Awareness Programming Guide, specifically the "Starting the Significant-Change Location Service" section.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215