I am trying to get the location updates Particularly when the application is in background. When the application enters background it should send the location updates for every n minutes
First I tried with the Foreground, it is working fine. I copied the same code to the applicationDidEnterBackground, I am unable to print the Location Updates. I don’t know where I am struck please help me.
in viewDidLoad
- (void)viewDidLoad {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
}
and
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
int degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
latLabel.text = lat;
degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
longLabel.text = longt;
}
I tried pasting these methods in the AppDelegate. It is not working 🙁 . Help me
You can’t simply paste the code there and expect your application to run in the background. You have to register it as an app that runs in the background. See the section Implementing Long-Running Background Tasks in this article