tags:

views:

929

answers:

4

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?

A: 

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

Azeem.Butt
+2  A: 

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.

mikestew
Seconded. Are you sure that that call is happening?
Sixten Otto
Yes, I tested it in the debugger and stopUpdatingLocation gets called after the self.locations.count reaches 4.
James Testa
A: 

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).

Jongsma
Yes, I tested it in the debugger and stopUpdatingLocation gets called after self.locations.count reaches 4.
James Testa
A: 

Hi James,

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.

lionfly