title: How can an iOS app keep a TCP connection alive indefinitely while in the background?
body: <p>An iPhone app, connecting to a remote server via TCP. The use scenarios are:</p>
  
  <ol>
  <li>app (user) sends data to server and server responds data back.</li>
  <li>server might send data to app while it does nothing.</li>
  </ol>
  
  <p>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.</p>
  
  <p>Case 1: if app is in foreground, I can use timer to send some do-nothing data to server. No problem.</p>
  
  <p><strong>Case 2</strong>: 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.</p>
  
  <p>I have read documentations about background execution, multitasking, and local notifications of iphone APIs. <strong>I'm not sure whether if I can achieve Case 2</strong>.</p>
  
  <p>Only use legal APIs, no jailbreaking.</p>