我的应用需要在后台跟踪用户位置,但无法发送"获取"请求.当应用程序到达前台时,会立即发送http请求.我正在使用RestKit来处理所有网络请求,我按照本教程设置了我的后台位置服务.在我的applicationDidEnterBackground中
-(void)applicationDidEnterBackground:(UIApplication *)application
{
self.bgLocationManager = [[CLLocationManager alloc] init];
self.bgLocationManager.delegate = self;
[self.bgLocationManager startMonitoringSignificantLocationChanges];
NSLog(@"Entered Background");
}
Run Code Online (Sandbox Code Playgroud)
我在applicationDidBecomeActive委托中停止监视有意义的位置变化
这是我的locationManager委托,我接受新的更新位置并发送到我的服务器
-(void) locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"I am in the background");
bgTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:
^{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
}];
// ANY CODE WE PUT HERE IS OUR BACKGROUND TASK
NSString *currentLatitude = [[NSString alloc]
initWithFormat:@"%g",
newLocation.coordinate.latitude];
NSString *currentLongitude = [[NSString alloc]
initWithFormat:@"%g",
newLocation.coordinate.longitude];
NSString *webToken = [[NSUserDefaults standardUserDefaults] stringForKey:@"userWebToken"];
NSLog(@"I am in the bgTask, my …Run Code Online (Sandbox Code Playgroud) 我可以在iOS4的后台向我的服务器发送位置更新吗?我想让用户更改位置,并使用Web服务调用将其发布到服务器.这里的主要问题是,当应用程序处于后台时,是否可以调用Web服务或http-post?