why self.locationManager stopUpdatingLocation does

2019-01-18 18:33发布

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.

9条回答
淡お忘
2楼-- · 2019-01-18 19:04

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!

查看更多
一夜七次
3楼-- · 2019-01-18 19:06

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!

查看更多
地球回转人心会变
4楼-- · 2019-01-18 19:06

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.

查看更多
登录 后发表回答