【发布时间】:2013-08-20 04:45:57
【问题描述】:
我正在使用 UISwitch 来“开启”和“关闭”定位服务。当我“打开”UISwitch 时,位置服务开始跟踪位置。我可以拿出应用程序,位置服务运行良好。我的问题是当我切换“关闭”跟踪并移动到任何View Controller 时,位置跟踪仍处于“开启”状态并且它没有停止跟踪。下面是我的代码。
-(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];
}
有什么建议吗?
【问题讨论】:
-
你用调试器测试的Cab,看看是否有[locationManager stopUpdatingLocation];被处决?
-
我用上面代码
-(IBAction)startTracking:(id)sender{的else部分验证了它会被执行,但下一行又开始跟踪 -
看看这个,可能是你的答案:stackoverflow.com/questions/9469741/…
-
我尝试了正确停止在该页面中的答案,但是当我保持 UISwitch“打开”并移动到下一页或上一页停止跟踪时
标签: iphone ios core-location cllocationmanager background-process