每小时轮询到TCP服务器ios

在我目前的应用程序中,我需要每小时通过TCP连接轮询到服务器。 我知道,最好的select之一是从服务器端使用推送通知,但我不能这样做。 所以目前我正在使用一个计时器,每9分钟启动一次,以保持应用程序在后台运行。 这工作正常..在小时我打电话给服务器的调查。

打开Tcp连接并生成轮询数据,但服务器没有响应。 这是因为当在后台应用程序不能运行需要几秒钟的代码块? 任何帮助将不胜感激,我也发布一些代码下面,

if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking IE iOS 4 if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking UIApplication *application = [UIApplication sharedApplication]; __block UIBackgroundTaskIdentifier background_task; background_task = [application beginBackgroundTaskWithExpirationHandler: ^ { UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil]; ViewController *controller = (ViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"viewController"]; [controller sendPoll]; }); } } 

然后编写输出数据的代码:

 NSData *data = [[NSData alloc] initWithData:[string dataUsingEncoding:NSASCIIStringEncoding]]; [_cacheArray addObject:string]; [_outputStream write:[data bytes] maxLength:[data length]]; 

最后streamDidReturn:

 -(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode { NSLog(@"event number %i ", eventCode); switch (eventCode) { case NSStreamEventOpenCompleted: NSLog(@"Stream opened"); break; case NSStreamEventHasBytesAvailable: if (aStream == _inputStream) { uint8_t buffer[1024]; int len; while ([_inputStream hasBytesAvailable]) { len = [_inputStream read:buffer maxLength:sizeof(buffer)]; if (len > 0) { NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]; if (nil != output) NSLog(@"server said: %@", output); } } } break; case NSStreamEventErrorOccurred: break; case NSStreamEventEndEncountered: break; case NSStreamEventHasSpaceAvailable: NSLog(@"space available"); break; default: NSLog(@"Unknown event"); } } 

可用的空间被调用,但没有进一步的服务器..

我似乎find了答案!

通过改变与服务器通信的function,应用程序保持活动状态的时间足够长,以发送和接收投票。 唯一的问题似乎是从appDelegate访问另一个ViewController,并期望它运行,如果在前台状态。

谢谢你对这个问题的重新编辑,这个谢谢,