【发布时间】:2012-12-03 14:32:31
【问题描述】:
在我们的应用中,我使用 CoreLocation 来获取用户的位置。我认为对 startUpdatingLocation 的调用每次都会调用 locationManager:didUpdateLocations。但是一旦第一次被调用,他们就再也不会被调用了。
在我的 AppDelegate.m 中:
- (void) startStandardUpdates {
if (_locationManager == nil) {
_locationManager = [[CLLocationManager alloc] init];
[_locationManager setDelegate:self];
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = 10;
[_locationManager startUpdatingLocation];
} else {
NSLog(@"[LOCATION]: Location Manager already exists.");
[_locationManager startUpdatingLocation];
}
}
然后通过 NSTimer 调用它:
- (void) startTask {
NSLog(@"Start task called.");
[NSTimer scheduledTimerWithTimeInterval:15
target:self
selector:@selector(actualTask)
userInfo:nil
repeats:YES];
}
- (void) actualTask {
NSLog(@"[TASK]: Starting location service...");
[self startStandardUpdates];
}
上面的代码应该每 15 秒调用一次 startUpdatingLocation,但第二次没有调用 didUpdateToLocation 方法(第一次调用成功......)。
有没有办法“以编程方式”调用(或强制)didUpdateToLocation?或者我们必须等到 didUpdateToLocation 获得位置?我已经搜索了每 x 分钟/秒获取用户位置的方法,但似乎没有人成功实现这一目标。我想做的只是相当于Android如何使用Service类(iOS没有这个)。
任何帮助将不胜感激。
谢谢。
【问题讨论】:
-
您对此有什么解决方案吗?我现在有同样的问题。但它只更新了五次。之后,它会自行停止。在模拟器中,它工作正常。请帮助我。
-
嗨。不幸的是,我的问题仍然没有“可靠”的解决方案。我不知道它是否有帮助,但在你的情况下,操作系统可能会因为后台任务超时而杀死你的逻辑?我曾经遇到过忘记在信息设置中检查“必需的背景模式”的情况......
-
我找到了解决问题的方法。在 iOS6 中,我们应该使用“didUpdateLocations”而不是“didUpdateToLocation”。那是什么问题。现在工作正常。
-
很高兴你解决了你的问题 :) 我也遇到了这个问题,并问了一个关于它的问题......不管怎样,继续努力,好好编码!
标签: ios core-location cllocationmanager cllocation