0

I am building an app in which the user can enter data while being online and offline. This data has to be synchronized with a server. If the user is online that's not really a problem. But now I need to send the data the user entered while being offline, the moment he gets an internet connection (for instance when he leaves a plane and the phone connects to the network).

The current solution I have in mind is using background services available on iOS and Android to poll the server every X minutes to see if the phone is online. This doesn't seem to be too much of a problem for Android. iOs however seems to be harder. According to this post, iOS app upload data to server as soon as network is available, it's not possible. But it seems iOS 7 has added new background capabilities which may make this use case feasible. I just can't find any good examples or documentation about it.

So the core question is, how can I detect the fact that the phone comes online even when my app is not being used at the moment (and might even have not been used at all since the last restart of the phone)?

Community
  • 1
  • 1
Peter Bierman
  • 363
  • 3
  • 16
  • I think I got confused. Is this question related to android or iOS? – Leo Sep 18 '14 at 03:01
  • Here is a [tutorial](http://www.appcoda.com/ios7-background-fetch-programming/) for background fetch in iOS7 but still this background fetch calling is totally depending on iOS so u can't have fixed time interval for making web service fire. – nikhil84 Sep 18 '14 at 03:51
  • Related to both actually. The functionality needs to be cross platform so I need a best strategy approach for both platforms. The strategy needed for iOS has a bit more priority because I couldn't find anything about how to handle this. – Peter Bierman Sep 18 '14 at 05:16
  • Thanks @walle84 this seems that it could work, only thing bugging me is that the functionality is clearly meant for fetching data from a server and not for pushing data to a server. It can probably be used for pushing as well but you don't seem to have a lot of control when this is going to happen. – Peter Bierman Sep 18 '14 at 05:18
  • how about you use the push notification for sending data to your app? Also how continuously your hitting server for data? If in every few min or hour then you should probably re-think as this will drain lot of battery and apple has put up concern on effective utilisation of battery. – nikhil84 Sep 18 '14 at 05:25
  • @walle84 in the ideal situation i am only calling the server when the network connectivity changes. I need to know if the user has added data to the app while in offline mode. – Peter Bierman Sep 19 '14 at 06:58
  • [Reachability](https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007324-Intro-DontLinkElementID_2) is there which let's u know that if connection is gone or alive. Also Afnetworking lib have them integrated in their lib. This would be helpful to u. – nikhil84 Sep 19 '14 at 07:52

2 Answers2

0

I've recently found a perfect piece of code to achieve the background fetch.

Note: When working with Background Fetch, there is a limitation that you should be aware about. The background execution time given to an application is not infinite. iOS provides a 30 seconds time frame in order the app to be woken up, fetch new data, update its interface and then go back to sleep again. It is your duty to make sure that any performed tasks will manage to get finished within these 30 seconds, otherwise the system will suddenly stop them. If more time is required though, then the Background Transfer Service API can be used

Code: DemoProject

Reference: ios7-background-fetch-programming

I hope it will be helpful to you.

Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57
0

Ios will not allow your app to work in minimized state for infinite time unless your app is a music player app, location based etc.

user2885077
  • 702
  • 6
  • 14