我在应用程序位于后台时发送位置时遇到问题。我正在使用CLLocationManager和startMonitoringSignificantLocationChanges。 postion didUpdateToLocation委托方法执行一次,但不是更多。我试着走过去,但没有新的位置被发送到服务器。当iphone位于后台时用CLLocationManager发送位置到服务器
我已经在info.plist文件中设置了“所需的背景模式” - >“位置更新的应用程序注册”。
任何人都知道什么可能是错的?从跟踪被启动,其中
代码:
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = appDelegate;
[appDelegate setLocationManager:locationManager withDistanceFilter:kCLDistanceFilterNone];
[appDelegate.theLocationManager startMonitoringSignificantLocationChanges];
代码(从CLLocationManagerDelegate):
- (void)setLocationManager:(CLLocationManager*)locationManager withDistanceFilter:(CLLocationDistance)distanceFilter {
// create a new manager and start checking for sig changes
self.theLocationManager.delegate = nil;
[theLocationManager release];
self.theLocationManager = locationManager;
self.theLocationManager.delegate = self;
self.theLocationManager.distanceFilter = distanceFilter;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSDate *newLocationTimestamp = newLocation.timestamp;
NSDate *oldLocationTimestamp = oldLocation.timestamp;
int locationUpdateInterval = 15;//15 sec
if (!([newLocationTimestamp timeIntervalSinceDate:oldLocationTimestamp] < locationUpdateInterval)) {
//NSLog(@"New Location: %@", newLocation);
[self updateToLocation:newLocation];
}
}
- (void)updateToLocation:(CLLocation *)newLocation {
NSLog(@"update location!!");
NSString *latitude = [NSString stringWithFormat:@"%f", [newLocation coordinate].latitude];
NSString *longitude = [NSString stringWithFormat:@"%f", [newLocation coordinate].longitude];
[currentUser updatePositionWithLongitude:longitude andLatitude:latitude];
}
多远你走动?根据您的位置,您可能需要覆盖更远距离 –
hm,不远处。我会尝试通过进一步行走来测试它。 1公里左右应该足够还是? – Madoc
也许只是将'startMonitoringSignificantLocationChanges'替换为'startUpdatingLocation'并查看它是否按预期运行......然后您将排除我读过的其他地方的其他问题 –