Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone - Connecting to server in background

I'm creating an app which connects to server and sends some text. If network (both wifi or 3g) is there, it will immediately send the text to server. But if there is no network, it keeps on polling for server connection every 5 minutes. All this part is working fine.

But when using iPhone 4 device, i want the app to check for server connection even when app goes into background. So, when app goes to background and when network comes back, it must be able to send the text to server.

How can I achieve it? I've seen some apps where they say that the app will upload photos to server even in background. How will they do it?

like image 966
Satyam Avatar asked Jan 04 '11 13:01

Satyam


2 Answers

I suggest you read this article from Apple carefully, especially the Completing a Finite Length Task in the Background section.

http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html

Something to clarify:

  • Once your app is in the background and is frozen by the OS, there would be no way for your app by it self to wake up and re-connect to the internet.
  • However, according to the article above from Apple, you can call this beginBackgroundTaskWithExpirationHandler method from your app's delegate to apply for additional time when put in the background, which is to say, though your app cannot wake up by it self when in background, it can, when in the background and not frozen, try to apply for additional time to finish its lengthy task.

Hope it helps.

like image 186
Di Wu Avatar answered Sep 23 '22 20:09

Di Wu


There is a trick that I think flayvr is using.

If you download and use the app, you will see that they require you to enable your location. And why is that? because they want like you to do something in the background even when the app is terminated (they creating an album out of your newly captured photos), and how do they do that?

They use the significant location change, where when someone is traveling some significant distance (something like 500m) each app that registered for significant location change will get awaken for a limited amount of time to perform some quick task and will be terminated in a few seconds.

So your app can register to that event also and when the event of significant location change fired you will be able to send the text to server (quickly).

Hope that helps.

like image 41
ozba Avatar answered Sep 24 '22 20:09

ozba