Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Location services don't stop when application is terminated

I'm currently developing an iPhone application which needs location services for various use including AR.

I test everything on simulator and on my iPhone 3GS and everything went well.

I recently tested on iPhone4 and on iPad2 and the location service (the little icon in status bar) keeps displaying even when I manually kill the app! The only way to disable this icon is to manually stop the location service for my app in the settings.

Does anyone know something about this? If needed I can post my code.

Thank you in advance

Edit :

When I kill the application, go to location services, switch off my app the location icon disappears. But when I switch it back on, it reappears! Is that normal?

like image 920
Geoffroy Avatar asked Sep 14 '11 08:09

Geoffroy


2 Answers

I've found the answer! It came from region monitoring, which I enabled before, but removed all code using it weeks ago.

As I had already tested on the iPad, and even if I deleted and re-installed the app, the system seems to have kept information on region I monitored.

Thus, as described by the documentation, the iOS kept on locating for my App, just as startMonitoringSignificantLocationChanges.

Thanks for you answers, it gave me a better understanding of the location system and how to efficiently use it (in particular thanks to progrmr and Bill Brasky)

like image 186
Geoffroy Avatar answered Oct 20 '22 01:10

Geoffroy


Sounds like you're app is going into the background and still using CLLocation. You can stop CLLOcationManager when you receive notification that you're app is resigning active, that's the best way. Then resume when it becomes active. The answer in this question show how to do that here

[EDIT] When your app goes into the background or resigns active for any reason (ie: phone call) you should stop location services at that time. You need to subscribe to the notifications and provide a method to stop and start location services, something like this:

-(void)appDidBecomeActiveNotif:(NSNotification*)notif
{
    [locationManager startUpdatingLocation];
}

-(void)appWillResignActiveNotif:(NSNotification*)notif
{
    [locationManager stopUpdatingLocation];
}

-(void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeActiveNotif:) name:UIApplicationDidBecomeActiveNotification object:nil];         
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActiveNotif:) name:UIApplicationWillResignActiveNotification object:nil]; 
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}
like image 29
progrmr Avatar answered Oct 20 '22 01:10

progrmr