2

I appreciate that this question already appears to have been answered here: How do I get a background location update every n minutes in my iOS application?

However, although the solution is hinted at, I'd be really grateful if someone could post some sample code as to how this actually works.

Many thanks,

Jack

Community
  • 1
  • 1
Jack Lenox
  • 357
  • 1
  • 5
  • 14

2 Answers2

0

You need to subscribe to core location during application start.

some thing like this

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self; 
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;

and add delegate

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
nswamy
  • 1,041
  • 9
  • 16
  • Sorry, I should have said. I've got the main part of this sorted. I've set up a button that, when pressed, gets the current location and passes a post request to a server. What I want is for this to then happen automatically once every 30 minutes when the app is in the background. – Jack Lenox Dec 02 '11 at 15:14
  • The core location delegate will be get called even when app is in background. – nswamy Dec 05 '11 at 11:18
  • Thanks for your help. This works but the problem is I want the updates to be quite far apart (e.g. once per hour) and this eats a lot of battery life. Is there a way of firing the location manager and then stopping it again with long intervals. – Jack Lenox Dec 12 '11 at 01:46
0

Try this one you will get the lattitude and longitude updated..in didUpdateToLocation...

//in view did load 

locationManager = [[CLLocationManager alloc]init];
    if([CLLocationManager locationServicesEnabled])
    {
        locationManager.delegate = self;
        locationManager.desiredAccuracy  = kCLLocationAccuracyKilometer;
        locationManager.distanceFilter = 1000.0f;
        [locationManager startUpdatingLocation];
        }


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
appDelegateObj.latitude= [[NSString alloc]initWithFormat:@"%g",newLocation.coordinate.latitude];
    appDelegateObj.longitude = [[NSString alloc]initWithFormat:@"%g",newLocation.coordinate.longitude];
  }
Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
Kartik
  • 779
  • 7
  • 22
  • Just posting the same comment as above. Thanks for your help: – Jack Lenox Dec 02 '11 at 15:16
  • Sorry, I should have said. I've got the main part of this sorted. I've set up a button that, when pressed, gets the current location and passes a post request to a server. What I want is for this to then happen automatically once every 30 minutes when the app is in the background. – Jack Lenox Dec 02 '11 at 15:16
  • so you can add button and on button press call this call thread for 30 min. so every 30 min thread will b call its own action and add this method in thread action so it will b call in every 30 min.. – Kartik Dec 03 '11 at 05:08