我有一个应用程序,捕获用户的位置,并将此信息发送到我的服务器。
我用以下命令启动了LocationManager服务:
- (CLLocationManager *)locationManager {
if (_locationManager == nil) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
isUpdatingLocation = NO;
}
return _locationManager;
}
-(void) startLookingForLocation{
[self locationManager].desiredAccuracy = kCLLocationAccuracyBest;
[self locationManager].distanceFilter = 250;
if([[self locationManager]respondsToSelector:@selector(setPausesLocationUpdatesAutomatically:)])
[[self locationManager] setPausesLocationUpdatesAutomatically:YES];
if([[self locationManager]respondsToSelector:@selector(setActivityType:) ])
[[self locationManager] setActivityType:CLActivityTypeFitness];
[[self locationManager] startUpdatingLocation];
[[self locationManager] startUpdatingHeading];
isUpdatingLocation = YES;
}并通过以下方式停止它:
-(void) stopLookingForLocation{
[[self locationManager] stopUpdatingLocation];
[[self locationManager] stopUpdatingHeading];
isUpdatingLocation = NO;
}因此,当我检测到用户位置发生变化时:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[self setCurrentLocation:newLocation];
BOOL isInBackground = NO;
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
{
isInBackground = YES;
}
if (isInBackground)
{
UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:
^{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
}];
// Send user-location to my server
if (bgTask != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
}
else
{
// Send user-location to my server
}
}一切工作正常。(我想)。
当应用程序进入后台模式时,我在appDelegate - (void)applicationDidEnterBackground:(UIApplication *)应用程序{ locationManager startMonitoringSignificantLocationChanges;}中调用此方法。
当app返回前台时:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[locationManager stopMonitoringSignificantLocationChanges];
}但我有一些疑问。这样,如果应用程序处于关闭状态,我的应用程序会继续与服务器通信位置吗?
当我处于后台或前台状态时,如何才能发送位置?
编辑:我还设置了:
UIBackgroundModes位置
发布于 2013-04-30 18:29:29
没有这个代码的需要,这将激活你的应用程序在后台很少的时间,你必须提到项目的info.plist中所需的后台模式,该值应为位置更新的应用程序注册。
只需在info.plist中设置UIBackgroundModes位置,它就可以在后台运行,这可能会对您有所帮助。
//In app deligate
#pragma mark -
#pragma mark GPS Service
//========== start GPS SERVICE ===========
-(void)startGpsService
{
locationManager=[[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=10.0;
[locationManager startUpdatingLocation];
[self performSelectorInBackground:@selector(sendDataToServer) withObject:nil];
}
#pragma mark -
#pragma mark CLLocation Manager Delegates
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
[self performSelectorInBackground:@selector(sendDataToServer) withObject:nil];
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// No need to do any thing
/* UIApplication *app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier bgTask = 0;
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
*/
}https://stackoverflow.com/questions/16297551
复制相似问题