我有一个应用程序可以捕获用户位置并将此信息发送到我的服务器。
我用:
启动了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
}
}
并且一切正常。 (我想)。
当app进入后台模式时,我在appDelegate中调用此方法 - (void)applicationDidEnterBackground:(UIApplication *)应用程序 { [locationManager startMonitoringSignificantLocationChanges]; }
当app返回前台时:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[locationManager stopMonitoringSignificantLocationChanges];
}
但我有些疑惑。这样,如果app处于关闭状态,我的应用程序会继续将位置发送到服务器吗?
如果我处于后台或前台状态时如何发送位置?
编辑: 我也设置了:
UIBackgroundModes 地点
答案 0 :(得分:2)
不需要此代码,这将在后台激活您的应用程序很少有时间您必须在项目的info.plist中提及所需的后台模式,并且值应该是App寄存器以进行位置更新。
仅在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];
}];
*/
}