Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone Turn off GPS with Objective-C

Tags:

iphone

gps

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?

like image 733
James Testa Avatar asked Dec 23 '22 08:12

James Testa


1 Answers

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.

like image 177
mikestew Avatar answered Jan 18 '23 23:01

mikestew