Question

Does anyone know how to turn off iPhone's GPS programmatically? Once I use the CLLocationManager to get three reads of my location I stop updating location as in the code below:

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation {

if((newLocation.horizontalAccuracy >  0.0f) &&
   (newLocation.horizontalAccuracy < 7000.0f) ){

    if(self.locations.count > 3){
        [self.locationManager stopUpdatingLocation];
    }
    [self.locations addObject:newLocation];
}

But this still seems to leave the GPS on while users are using my app and draining their battery. All I need to do is read the location three times so that I can get an accurate read, and then shut down the GPS. Does anybody know how to shut down the GPS with objective-C?

Was it helpful?

Solution

stopUpdatingLocation give the location manager the option to shutdown the hardware, but it is not guaranteed. It is supposed to shut down the hardware when no one else needs it. In practice, it seems to work as one would expect.

Is it possible stopUpdatingLocation just never gets called? In your snippet above, it does not look to be unreasonable that your code never makes it to that call.

OTHER TIPS

I wonder whether you need an extra line to make it to:

[locationManager stopUpdatingLocation];
locationManager.delegate=nil;

to stop the location manager. This is what I use and many places have these 2 lines together. Good luck.

Apple docs says that Location manager will turn off GPS hardware if your location accuracy more than one kilometer:

For example, setting the desired accuracy for location events to one kilometer gives the location manager the flexibility to turn off GPS hardware and rely solely on the WiFi or cell radios. Turning off GPS hardware can lead to significant power savings. From CLLocation class references

Be aware you should use only standart (not significant) location changes tracking.

Third party applications aren't supposed to touch hardware settings, so I'm going to guess you can't do this.

The stopUpdateLocation method should have the desired effect. Have you tried whether it is being called at all (you can do this by setting a breakpoint at the line).

With me, it was my map instance showing the user location that held the GPS going. Could be a tip to anyone who tries all the above without any luck.

Here's my stopGPS() function:

-(void) stopGPS
{
   NSLog(@"Stopgps");

   myMap.delegate = nil;
   myMap.showsUserLocation = NO;
   myMap = nil;

   [locationManager stopMonitoringSignificantLocationChanges];
   [locationManager stopUpdatingHeading];
   [locationManager stopUpdatingLocation];
   locationManager.delegate = nil;
   locationManager = nil;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top