An iPhone app, connecting to a remote server via TCP. The use scenarios are:
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 make phone calls using an Internet connection instead of the device’s cellular service. Such an application needs to maintain a persistent network connection to its associated service so that it can receive incoming calls and other relevant data. Rather than keep VoIP applications awake all the time, the system allows them to be suspended and provides facilities for monitoring their sockets for them. When incoming traffic is detected, the system wakes up the VoIP application and returns control of its sockets to it.
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.
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.
For when the app is in the foreground:
Look at STOMP http://stomp.codehaus.org/Protocol
The STOMP Framework in Obj-C http://code.google.com/p/stompframework/
and the ActiveMQ broker http://activemq.apache.org/
For when the app is in the background I would probably go for using APNS (Notifications) to prompt the user to wake the app back up...
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"