我正在使用区域监控构建应用。它在前台工作正常但是一旦应用程序在后台发送,它就不会按预期运行:它会调用didEnter和didExit但是只要它开始执行回调它就会停止。在这些回调方法中,我需要轮询服务器并保持didExitRegion和/或didEnterRegion状态。只要我再次将应用程序置于前台,任何排队的请求就会启动并完成。 任何的想法? 我在iphone 4上使用ios5.1和ios6
答案 0 :(得分:2)
当您在后台调用
时- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
(或......退出)
只需设置服务器调用所需的任何内容(变量,服务器的有效负载等)。 在实际发送开始之前
self.bgTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:self.bgTaskId];
self.bgTaskId = UIBackgroundTaskInvalid;
somelogger(@"ran out of time for background task");
// remember that we failed for the next time the app comes to the foreground
}];
然后使用您选择的HTTP框架进行实际发送,并在完成处理程序中重置后台任务
[[UIApplication sharedApplication] endBackgroundTask:self.bgTaskId];
使用AFNetworking的示例:
[self.httpClient postPath:@"state" parameters:@{@"abc": abc, @"value": val, @"h": h, @"app":myAppName , @"version": myAppVersion }
success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (operation.response.statusCode != 200) {
DDLogVerbose(@"response was not 200. error: %i", operation.response.statusCode);
} else {
DDLogVerbose(@"success");
}
[[UIApplication sharedApplication] endBackgroundTask:self.bgTaskId];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DDLogVerbose(@"request error %@, current retry count %d", error, retryCount );
// start our own retry mechanism
if (retryCount < MAX_RETRIES) {
retryCount++;
double delayInSeconds = RETRY_INTERVAL * (1 + (double)retryCount/10);
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// try again
});
} else {
// final
[[UIApplication sharedApplication] endBackgroundTask:self.bgTaskId];
// remember failure when app comes back to foreground
}
}];
我正在使用
@property (assign, nonatomic) UIBackgroundTaskIdentifier bgTaskId;
存储背景标识符。
中解释答案 1 :(得分:0)
答案 2 :(得分:-1)
如果您尚未将“location”添加到Info.plist的UIBackgroundModes中。作为第二个想法,我使用AFNetworking,这是广泛流行并具有后台支持。这意味着它将处理设置参数以告诉操作系统它将“在我回去睡觉之前完成这件事”。