1

I'm trying to understand how I can send my offline data to the server when the device is online.

Can this be achieved without having the user to open the app, and send the data immediately when the device is connected to the Internet in background?

Joel Fernandes
  • 4,776
  • 2
  • 26
  • 48
  • hmmm I don't think so. The app should be opened. See related question http://stackoverflow.com/questions/35124815/send-data-to-server-whenever-internet-connection-is-available-ios – mownier Feb 23 '16 at 06:30

1 Answers1

1

One viable approach would utilise file uploads with a "background session".

What it requires is a file that represents your Core Data objects. When you have that, create a NSURLSession object with a background configuration using static function backgroundSessionConfigurationWithIdentifier(_:).

When you schedule a task with the background session, it will be executed by the system in the background, even when your app is terminated. The system only tries to download the file when it has a connection.

For more information see Using NSURLSession and Handling iOS Background Activity.

There are a few configuration options (NSURLSessionConfiguration) which may seem important in your case, too:

discretionary and sessionSendsLaunchEvents

CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67
  • Thanks for the info. I believe that Apple by default does not allow one to do this unlike how it's in Android with BroadcastReceiver. I also checked on GMail to see if the app sends an pending email from the outbox. Turns out that unless the user opens the app, pending emails are not sent. – Joel Fernandes Feb 25 '16 at 16:41