26

Problem: It seems I can't stop Core Location from sending updates to my app / tracking.

What am I doing: In my viewWillAppear I call self.locationManager and pass it to a method to show user's location on the map (an instance of MKMapView). The getter is overridden to check for availability of the serive, to see whether its authorized. If so, it allocs/inits and startMonitoringSignificantLocationChanges and returns.

In viewDidDisappear, I call [self.locationManager stopUpdatingLocation]. But still I can see the location icon in the status bar. Even when I terminate the app by double tapping the home button and closing it, the icon is still there... even after extended amount of time (10+ minutes). When I go to the Settings App, it tells me that my app is still using location services (purple icon).

Question: What am I missing here? Why location update doesn't stop?

Thanks in advance.

Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49
Sierra Alpha
  • 3,707
  • 4
  • 23
  • 36

9 Answers9

16

The opposite of startMonitoringSignificantLocationChanges is not stopUpdatingLocation, it is stopMonitoringSignificantLocationChanges.

You probably want to replace startMonitoringSignificantLocationChanges with startUpdatingLocation for the sake of more regular updates, unless you have a specific reason for monitoring only for significant location changes.

Check out the CLLocation documentation for further detail.

jnic
  • 8,695
  • 3
  • 33
  • 47
  • I tried both. In fact, it was stopMonitoringSignificantLocationChanges when I first wrote the code, but since it didn't stop updating, I changed it to stopUpdatingLocation. I'm trying to show location of certain type of business on the map, so I only need user's location once. I don't need to track it. – Sierra Alpha Feb 27 '12 at 18:20
  • 2
    I'd still advocate using `startUpdatingLocation` for your scenario, calling `stopUpdatingLocation` in your `CLLocationManagerDelegate` once you have a location with sufficient accuracy for your needs. – jnic Feb 27 '12 at 18:23
  • 2
    I see this answer is checked as correct. jnic's response is valid, but I suspect the response I provided below is the correct one. – Christopher Apr 08 '12 at 18:12
  • My app still seems to track indefinitely, even after I have called [stopUpdatingLocation]...From the Apple docs it seems that that call actually "allows" CLLocationManager to stop tracking if it wants to, e.g. if no other classes are listening to it I suppose. My app just needs to get a good initial fix and then I'd like to turn location services off for the duration...but I can't seem to achieve this. I don't use a map view at all. – Reid Dec 05 '13 at 17:45
  • In viewDidDisappear, `[self.locationManager stopUpdatingHeading];` `self.locationManager = nil;` worked for me – EarlySun Apr 20 '17 at 07:32
13

I too just experienced the same problem as Canopus. It appears that even if stopUpdatingLocation is called the navigation pointer still resides on the status bar if I have showUserLocation enabled. Perhaps this is a bug? It may be as I am running and testing with iOS 4.2.1 and this issue may have been fixed in a later SDK.

I would think that if stopUserLocation is called it would also stop showing the user location since the view I am using it in has already disappeared and is subsequently deallocated.

It appears that you must set showUserLocation to NO before stopping user location updates.

Anyway, in my viewDidLoad method I have the following code:

self.mapView.showsUserLocation = YES;

More code...

- (void)viewWillDisappear:(BOOL)animated
{    
    if (locationManager)
    {
        mapView.showsUserLocation = NO;
        [locationManager stopUpdatingLocation];
    }

    [super viewWillDisappear:animated];
}

- (void)dealloc
{
    if (locationManager)
    {
        [locationManager release];
        locationManager = nil;
    }

    (other code)
}
Christopher
  • 5,806
  • 7
  • 31
  • 41
  • 7
    This is because stopUpdatingLocation just tells the LocationManager that your locationManager is no longer interested in location updates. mapView has its own locationManager that is still requesting location updates while showsUserLocation is enabled. GPS hardware isn't turned off until there's nothing waiting for location updates anymore, so the indicator stays on. – aranasaurus Feb 14 '14 at 18:10
  • Works for me also. You can see in the Debug Navigator/Energie Impact, that Locationservice is turned off. The icon will show some seconds longer the use of GPS. – Gondomir Mar 20 '16 at 01:22
5

Swift:

Your map AND location manager both need to be stopped:

override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        locationManager.stopMonitoringSignificantLocationChanges()            
        locationManager.stopUpdatingLocation()
        mapView.showsUserLocation = false

    }

You can debug/check location services usage right there in Xcode, in the debug navigator under Energy Impact.

Juan Boero
  • 6,281
  • 1
  • 44
  • 62
  • For my purposes, I also turn off the showsUserLocation when the app goes into background (listen for notification `.UIApplicationDidEnterBackground`) and turn it back on when I get `.UIApplicationDidBecomeActive` and the view is shown (`if self.isViewLoaded && (self.view.window != nil) `). In my case, the map view using the GPS is independent of when the app uses location manager for other purposes in foreground and background. On a device, the location "arrow" shows that it turns off after about 10 seconds. – James Apr 30 '17 at 02:04
2

I solved this setting nil to locationManager property after delegate

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    // Your code getting coordinates 
    //Here set nil
    locationManager = nil;

}
iOScar182
  • 29
  • 2
  • That won't be able to set locationManager to nil, I got a type mismatch error when I am trying to set its value to nil. – crazy_phage Sep 03 '15 at 13:33
  • please show more code regarding locationManager, is it a variable? where in the docs says we should nil the manager? – Juan Boero Mar 21 '17 at 19:37
1

My app requests "always" auth. Only a certain feature within the app requires that. If the user turns that feature off then on app close we can stop location updates (with the goal of saving battery and removing the location icon from the status bar). I too thought stopping was not working because after app close the location icon in the status bar was still there even though my app was the only app running on my phone with location access and "on app close" I just told it to stop updating locations.

For me the solution was to be a bit more patient and then I realized that it takes iOS about 10-15 seconds to turn off location hardware and update the status bar. I didn't have to nil out my location manager or anything special, just stopped updates on app close, waited 15 seconds, and observed iOS remove the location icon from the status bar. Hope this helps somebody out there!

John Erck
  • 9,478
  • 8
  • 61
  • 71
1

I was working with CLLocationManager in Swift and I think is relevant to the either Swift or Objective-C but, I just created a boolean which I update to true whenever I have received the location update. Since in my case I just need it once on launch.. Example,

// my boolean to stop location updates
var alreadyUpdatedLocation = Bool()

Also, in my case I have created a helper function that whenever I get the data/location of the user, I just call my own stopUpdatingLocation like this,

// helper function to stop updates
func stopUpdationgLocation() {

    // since I received my data within a block, I don't want to just return whenever it wants to :)    
    dispatch_async(dispatch_get_main_queue()) {

        // the building stop updating location function call
        self.locationManager.stopUpdatingLocation()

        // my own trick to avoid keep getting updates
        self.alreadyUpdatedLocation = true

    }
}

Then, where ever you use the location updates that you have received, you could do this...

// avoiding multiple calls after I have received user location
if(self.alreadyUpdatedLocation) {
        return
}

I hope it helps!

valbu17
  • 4,034
  • 3
  • 30
  • 41
0

try this..

    if ([CLLocationManager significantLocationChangeMonitoringAvailable])
            {
                [self.locationManager startMonitoringSignificantLocationChanges];


            }

 [[self locationManager] stopUpdatingLocation];
Jay Bhalani
  • 4,142
  • 8
  • 37
  • 50
Kiran K
  • 919
  • 10
  • 17
0

If you are using the GoogleMaps SDK for iOS, I found that if you call

locationManager.stopUpdatingLocation() 

and still have

mapView.isMyLocationEnabled = true

then the gps icon remains.

What I chose to do is initially show the user's location on the map and then turn it off after 8 seconds. This worked for me using the GoogleMaps SDK. I added this in ViewDidLoad:

DispatchQueue.main.asyncAfter(deadline: .now() + 8.0, execute: {
            self.locationManager.stopUpdatingLocation()
            self.mapView.isMyLocationEnabled = false
        }) 

You could put the same code in viewWillDisappear if you prefer to not have the gps icon in the status bar when you segue to another view.

D. Rothschild
  • 659
  • 9
  • 14
0

Problem: In my case I use both startMonitoringSignificantLocationChanges and startUpdatingLocation. Even after stopping the location through locationManager.stopMonitoringSignificantLocationChanges() & locationManager.stopUpdatingLocation(). My location is getting called continuously.

Solution: 1. Check whether you have invalidated the timers. 2. Initialize locationManager.delegate = nil.

These will surely solve your problem.

Sarthak Sharma
  • 246
  • 2
  • 11