iosbackground-processvoipgcdasyncsocketnsnetservice

iOS: How to keep TCP socket and the NSNetService working when the App is in the background


I'm doing a P2P VoIP application using GCDAsyncSocket, GCDAsyncUdpSocket and NSNetService to find other devices with the application on the network. I have a TCP socket to transfer user data and negotiate the UDP connection.

I need to keep the NSNetService service in publishing Bonjour while the App is in the background and keep an open socket for receiving requests from other devices.

Pressing the HOME button of the iPhone, the socket does not disconnect but does not respond and the data is sent the application only when the app is reopened. By blocking the screen, the socket is closed.

The NSNetService to publish when the application is closed.

I activated the options of background VoIP App, Audio, etc.

I used

 [ asyncSocket performBlock : ^ {
        [ asyncSocket enableBackgroundingOnSocket ];
    }];

Someone knows how to keep the Socket and the NSNetService working when the iPhone is locked?


Solution

  • I bolded what I think is important.

    From Apple: https://developer.apple.com/library/ios/technotes/tn2277/_index.html#//apple_ref/doc/uid/DTS40010841-CH1-SUBSECTION15

    VoIP Sockets A VoIP (Voice over IP) app is expected to run continuously so that it can monitor the VoIP control connection; however, to minimize the memory impact on the system, the app is suspended while it's inactive. To make this work the app must register the data socket for its control connection as a VoIP socket. A socket registered in this way has two special features:

    While the app is suspended the system monitors the socket on the app's behalf. If any data arrives on the socket, the system resumes execution of the app (albeit in the background), which can then read the data off the socket and take the appropriate action (for example, notifying the user of an incoming call). The socket's resources are never reclaimed. Thus, the app can safely be suspended without any fear of its socket going deaf. For more information about how to create a VoIP app, see the iOS Application Programming Guide.

    Also you can reference this: How to Maintain VOIP socket connection in background?