Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

An iPhone app, connecting to a remote server via TCP. The use scenarios are:

  1. app (user) sends data to server and server responds data back.
  2. server might send data to app while it does nothing.

Assume that if app does not send data to server for 30 minutes, server will close the connection. I want to keep the connection alive for 120 minutes even if user does nothing.

Case 1: if app is in foreground, I can use timer to send some do-nothing data to server. No problem.

Case 2: if user pressed Home and app went to background, what could I do? I don't wan to show alert or something to interrupt user(he is away or playing games). I just wanna keep the connection alive for a longer period and when user comes back to the app, he found out that the connection will be still alive and be happy with that.

I have read documentations about background execution, multitasking, and local notifications of iphone APIs. I'm not sure whether if I can achieve Case 2.

Only use legal APIs, no jailbreaking.

like image 693
yehnan Avatar asked Apr 30 '11 07:04

yehnan


People also ask

How long can an app stay in background?

A reasonable time limit would be 30 or 60 minutes I think. Let the Android app lifecycle handle it. Basically, when Android decides to kill the app process, forget about scroll state. My problem here is that I don't know how fast this usually happens, and it might vary depending on the phone performance.

How long will an iOS app run in the background?

Tasks are under a strict time limit, and typically get about 600 seconds (10 minutes) of processing time after an application has moved to the background on iOS 6, and less than 10 minutes on iOS 7+.

What is inactive state in iOS?

Inactive - The app is running in the foreground, but not receiving events. An iOS app can be placed into an inactive state, for example, when a call or SMS message is received. Active - The app is running in the foreground, and receiving events. Background - The app is running in the background, and executing code.


2 Answers

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.

like image 180
Nick Avatar answered Sep 30 '22 15:09

Nick


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.

like image 23
Nick Weaver Avatar answered Sep 30 '22 16:09

Nick Weaver