1

Am using UISwitch to turn "ON" and "OFF" the the location services. When i Switch "ON" the UISwitch the location services start to to track teh location. I can come out the app and location services are running good. My problem is when i switch "OFF" the tracking and move to any View Controller the location tracking still in "ON" state and its not stopping the tracking. below is my code.

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    CLLocation* location = [locations lastObject];
    latDeg = location.coordinate.latitude;
    longDeg = location.coordinate.longitude;
}

-(IBAction)startTracking:(id)sender{
 if(startTrackingButton.on){
[locationManager startUpdatingLocation];
[[NSUserDefaults standardUserDefaults] setBool:startTrackingButton.on forKey:@"switchValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else{
    [[NSUserDefaults standardUserDefaults] setBool:startTrackingButton.on forKey:@"switchValue"];
  [[NSUserDefaults standardUserDefaults] synchronize];
  [locationManager stopUpdatingLocation];
 }
}

- (void)viewDidLoad{
   [super viewDidLoad];
   [startTrackingButton setOn:[[NSUserDefaults standardUserDefaults] boolForKey:@"switchValue"]];
   locationManager = [[CLLocationManager alloc] init];
   [locationManager setDelegate:self];
   [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
   [self.navigationItem setHidesBackButton:YES];
}

Any suggestions?

Shekhar Gupta
  • 6,206
  • 3
  • 30
  • 50
Sabarish
  • 1,184
  • 3
  • 14
  • 35
  • Cab you test with the debugger to see if the line [locationManager stopUpdatingLocation]; gets executed? – danh Aug 20 '13 at 05:13
  • I verified with the else part of `-(IBAction)startTracking:(id)sender{` above code it gets executed but the next line again it starts to track – Sabarish Aug 20 '13 at 05:39
  • Check this out, probably your answer: http://stackoverflow.com/questions/9469741/why-self-locationmanager-stopupdatinglocation-doesnt-stop-location-update – danh Aug 20 '13 at 06:07
  • i tried with the answer it stopped in that page correctly but when i kept the UISwitch "ON" and move to next page or previous one in stops tracking – Sabarish Aug 20 '13 at 07:02

1 Answers1

0

I'm going to assume that startTrackingButton is actually a UISwitch. If that is the case, you should be checking the isOn property.

if (startTrackingButton.isOn) {
    [locationManager startUpdatingLocation];
    [[NSUserDefaults standardUserDefaults] setBool:startTrackingButton.on forKey:@"switchValue"];
    [[NSUserDefaults standardUserDefaults] synchronize];
} else {
    [[NSUserDefaults standardUserDefaults] setBool:startTrackingButton.on forKey:@"switchValue"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [locationManager stopUpdatingLocation];
}
Bill Burgess
  • 14,054
  • 6
  • 49
  • 86