title: GCDAsyncSocket background VOIP
body: <p>I'm trying to implement <a href="http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedAppTricks/AdvancedAppTricks.html#//apple_ref/doc/uid/TP40007072-CH7-SW12" rel="nofollow">this</a>(Configuring Sockets for VoIP Usage) using <a href="https://github.com/robbiehanson/CocoaAsyncSocket" rel="nofollow">this</a>(CocoaAsyncSocket).  To the best of my knowledge step 1 I have done, adding VOIP to background services in the plist, and below should be step 2 (Configure one of the app’s sockets for VoIP usage)</p>
  
  <pre><code>- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)connectedPort
  {
      [CCV setLocalMode:FALSE];
  
      [socket performBlock:^{
                   [socket enableBackgroundingOnSocket];
               }];
  </code></pre>
  
  <p>But the rest of the steps I cannot figure out.
  If I do </p>
  
  <pre><code>- (void)applicationDidEnterBackground:(UIApplication *)application
  {
       expirationHandler = ^{
  
            [app endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;
  
  
            bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
       };
  
       bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
  
  
       // Start the long-running task and return immediately.
       dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  
            while (1) {
                 sleep(1);               
                 //NSLog(@"BGTime left: %f", [UIApplication sharedApplication].backgroundTimeRemaining);
  
                 if ([rootViewController isIncomingCall] &amp;&amp; showedCall != TRUE) {
                      UILocalNotification *localNotif = [[UILocalNotification alloc] init];
                      if (localNotif) {
                           localNotif.alertBody = [NSString stringWithFormat:@"Incoming Call."];
                           localNotif.alertAction = NSLocalizedString(@"Accept Call", nil);
                           localNotif.soundName = @"alarmsound.caf";
                           //                         localNotif.applicationIconBadgeNumber = 1;
                           [application presentLocalNotificationNow:localNotif];
                           [localNotif release];
                      }
                      showedCall = TRUE;
                 }
            }  
       });      
  
  }
  </code></pre>
  
  <p>My client will listen for 10mins then stop.  I need to do 2 things: watch for a specific (incoming call) data packet, and send a keepalive every 5seconds.  But I dont see where these things are to be configured at.  Furthermore the definition on the apple link above states <em>"To prevent the loss of its connection, a VoIP app typically needs to wake up periodically and check in with its server. To facilitate this behavior, iOS lets you install a special handler using the <code>setKeepAliveTimeout:handler:</code> method of <code>UIApplication</code>"</em>  From my understanding of that this timeout thing should only be doing the keepalive, however that doesnt make sense because the minimal time is 600seconds?  And like I said above I cannot find where I tell it what packet to look for as well as what to do when its found.</p>