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?