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条回答
Viruses.
2楼-- · 2019-01-18 18:40

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;

}
查看更多
迷人小祖宗
3楼-- · 2019-01-18 18:45

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.

查看更多
神经病院院长
4楼-- · 2019-01-18 18:46

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.

查看更多
狗以群分
5楼-- · 2019-01-18 18:54

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)
}
查看更多
小情绪 Triste *
6楼-- · 2019-01-18 18:55

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.

查看更多
冷血范
7楼-- · 2019-01-18 19:01

try this..

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


            }

 [[self locationManager] stopUpdatingLocation];
查看更多
登录 后发表回答