在我们的应用程序中,我使用 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没有这个)。
任何帮助,将不胜感激。
谢谢你。