How can an iOS app keep a TCP connection alive indefinitely while in the background? How can an iOS app keep a TCP connection alive indefinitely while in the background? ios ios

How can an iOS app keep a TCP connection alive indefinitely while in the background?


Tapbots solved this problem with Pastebot by prompting the user to run a silent background audio track at all times.

Note that Apple frowns on using hacks like employing the background audio or VOIP APIs to keep non audio or VOIP apps running (as evidenced by the 'workaround' described in the article above) so dabbling with these techniques risks rejection at the point of submission.

Unfortunately, though, there is no legal API to keep a connection alive in the background. Perhaps they'll introduce one in a future update to iOS, but you might consider submitting a feature request to voice your support for it.


Please have a look at Implementing a VoIP Application in Apple's iOS Application Programming Guide. This is no tested solution, but I think it can be used to achieve what you are asking for.

Implementing a VoIP Application

A Voice over Internet Protocol (VoIP)application allows the user to makephone calls using an Internetconnection instead of the device’scellular service. Such an applicationneeds to maintain a persistent networkconnection to its associated serviceso that it can receive incoming callsand other relevant data. Rather thankeep VoIP applications awake all thetime, the system allows them to besuspended and provides facilities formonitoring their sockets for them.When incoming traffic is detected, thesystem wakes up the VoIP applicationand returns control of its sockets toit.

I am not sure if you're developing a VoIP app but you'll be able to access the sockets and react on incoming packets. The text after the quote refers to [setKeepAliveTimeout:handler:][2] which allows you to set a block of code which allows you to keep alive a(your VoIP) connection. Minimum scheduling time is 10 minutes, though.

In Implementing a VoIP Application is a paragraph on Installing a Keep-Alive Handler. I am curious if this will help. Please give us a note if this solved your problem.


1.Create your socket with VOIP property like this

[inputStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType] ;[outputStream  setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType] ;

2.Add required background mode as "App provides Voice over IP services" in app.plist

If your application in background mode the voip socket will not close. Using this socket with uilocal notification you can get.

"If your application is not an original voip application app store will reject"