1

I have a messaging app. Every time I try to send a message without internet connection, backgrounding my app after it, it will not send the message when the internet connection is recovered (unless I put my app in foreground again).

I have a reachability test that tries to send the messages again as soon as the connection is reestablished. However, the reachability test stops working as soon as the app is put on background.

I know it seems like a duplicate, but all the responses I could find in stack overflow says that it is impossible to execute things in background on iOS. The iOS documentation says the same. However, Telegram can handle that somehow, so I'm sure there is a way to do that.

The test I'm doing with telegram is:

  1. Put in airplane mode
  2. Send a message
  3. Background telegram
  4. Wait for a minute. It does not matter how much time you wait, but I just want to be sure that the app is already terminated.
  5. Unselect airplane mode
  6. The message will be sent with the app in background

How is telegram executing code in background whenever it wants to?

Gabriel Gava
  • 571
  • 4
  • 10
  • Read [this chapter of Apple's Programming Guide](https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html) but preferably read the complete Guide, the TOC is on the left. There are also a zillion posts about this here on SO, there are YT videos and good online tutorials (on Ray Wenderlich's website and many others). – meaning-matters Aug 08 '17 at 17:30
  • If you use a background `URLSession`, the OS will send that request when it can, even if app isn't running at that time (with some limitations on the timing of when the OS chooses to do this, namely whether the device is on wifi, connected to power, whether the cellular data connection is spun up for other processes; etc.). But going through the details of background sessions is beyond the scope of this question: Research background `NSURLSession`/`URLSession` and only come back if you have questions after going through those tutorials/discussions. – Rob Aug 08 '17 at 18:39
  • Thanks for the hints @meaning-matters. I already read the guide and Wenderlich's website. I also checked a lot of SO responses. I will check YT too. – Gabriel Gava Aug 09 '17 at 15:06
  • Thanks @Rob. I am using AFNetworking to do so. It implements HTTP using URLSession. I tried configuring it with a background session. It just calls its failure callback as soon as it tries to do the POST and the network is offline. I also tried using URLSession directly, with background configuration, but it also did not work (I know this may be my fault). I will try it again, so I can bring more concrete information for you. Thanks for now. – Gabriel Gava Aug 09 '17 at 15:06
  • First, if you have an error, look at what that error says. Second, in answer to your question, if you want to use AFNetworking with background session, see https://stackoverflow.com/a/21359684/1271826 for an example. Note the use of download task rather than data task. (And before you say "hey, I don't need to download anything", I know, but you presumably want to process the response to confirm that the request was successfully received and processed; and you do that with download task, and then parse the response in the body of the "download".) – Rob Aug 09 '17 at 16:18

0 Answers0