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.

  • iPhone4 iOS5 battery level monitoring do I need to add setBatteryMonitoringEnabled:NO to periodic battery checks?
  • Images for retina display iphone
  • Xcode: Could not locate device support files
  • How could I create a shortcut on desktop in iOS through an app
  • Compensating compass lag with the gyroscope on iPhone 4
  • Enabling the photo library button on the UIImagePickerController
  • 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?

    7 Solutions Collect From Internet About “Location services don't stop when application is terminated”

    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)

    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];
    }
    

    I ran into this same exact issue when using the region monitoring tools. It didn’t matter what I did to disable the regions, the arrow remained. I did finally solve the issue by cleaning up the calls to locationManager. If you are closing your view and don’t need the location manager, set it to nil and/or release it. If you are monitoring location in the background, it will stay up there, but if not, make sure you are cleaning up all your location monitoring.

    It seems like it is a bug, but as I found out, it is not. Just requires a bit more cleanup.

    I have been battling with this problem for a while, and I think I’ve finally gotten to the bottom of it..

    The reason that the location service doesn’t stop when you ask it to is not because you haven’t stopped it or released it properly. It’s actually caused by releasing and re-allocating the CLLocationManager itself, in my experience.

    If you have code which releases your CLLocationManager in applicationDidEnterBackground, and then you allocate a brand new one in applicationDidEnterForeground, etc., then you’ll probably have this problem.

    The solution is this:

    1. Only create your CLLocationManager object once, in applicationDidFinishLaunching.
    2. To start, call startUpdatingLocation, startMonitoringSignificantLocationChanges etc. as normal.
    3. To stop updates, call the appropriate stopUpdatingLocation, stopMonitoringSignificantLocationChanges etc. as normal.
    4. Never, ever release your CLLocationManager or set its’ reference to nil (except possibly in applicationWillTerminate, but that probably won’t make any difference).

    This way, I went from having my app continue to use location services for up to 12 hours after putting my app away in the background, to the location services arrow disappearing within 10 seconds of backgrounding with this new approach.

    Note: Tested on iPhone 4S running iOS 5.1.1. To get accurate results on your app’s performance in this regard, make sure you go into Settings->Location Services->System Services and turn off the Status Bar Icon switch. That way, the status bar arrow will accurately reflect usage by apps alone.

    Presumably this is so users don’t need to stare at the bar to notice some mischievous app is using location services. That icon appears when you use any location services and remains for some indeterminate time afterwards.

    This is intentional behavior Apple wants users to know which apps are using their locations. It seems this is sensitive data, wouldn’t you agree?

    This is the solution which fixed this problem for me.

    Just stop monitering location changes in

    - (void) applicationDidEnterBackground: (UIApplication *)application
    {
        [locationManager stopMonitoringSignificantLocationChanges];
        locationManager.delegate = nil;
    }
    

    not in applicationWillEnterForeground: .Still,it takes a few seconds to disappear locating icon.

    I don’t know why it isn’t working in the latter method.

    I’ve run into that problem a while ago and found it useful to apply only one method of applicationDelegate object

    - (void)applicationWillEnterForeground:(UIApplication *)application;
    

    If you’ll stop your CLLocationManager from receiving updates inside that call, you’ll be alright. Of course you’ll need to start updating somewhere else, and - (void)applicationDidBecomeActive:(UIApplication *)application; will be a good choice. Also you need to note, that there are two methods of location awareness

    • the gps based -(void)start/stop_UpdatingLocation;

    • and the 3g/wi-fi based -(void)start/stop_MonitoringSignificantLocationChanges;