Internet state changes with code executing in the background

I need to execute a code to restart a connection, webRTC one, just after the internet is back and my app is in the background. How is this possible? Can I listen to internet state changes while in the background? Can I run a callback to reconnect my app when the listener triggers?

The app is a push-to-talk that uses webRTC, that's why it should be able to reconnect if the internet falls for a bit.

Thanks! Peace, Moisés!

I need to execute a code to restart a connection, WebRTC one, just after the internet is back and my app is in the background.

In most cases iOS suspends your app after moving it to the background. If your app is suspended, it can’t do anything with the network [1]. However…

The app is a push-to-talk that uses WebRTC

Presumably this means that your app has found a way to execute in the background. How does that work?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Other than NSURLSession background session stuff, which is done by a system daemon on your behalf.

Hi, Quinn! Thanks for the reply!

RTCAudioSessionConfiguration *webRTCConfiguration = [RTCAudioSessionConfiguration webRTCConfiguration];
  webRTCConfiguration.categoryOptions = (AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers);

This piece of AudioSession code and Background Mode [Audio, Airplay, etc & Voice over IP], make things work properly with the push-to-talk call.

We have an old app that does this reconnection automatically, but the code is a mesh and I really can't see how it works. In the foreground, it sounds like it never stops to try to reconnect even without the internet and even backgrounding. Something like really bad practice. But it works.

The point is: it's important for the app to reconnect automatically. How can I do this? Notifications?

Thanks! Peace, Moisés!

So your audio session is preventing your app from suspending?

it sounds like it never stops to try to reconnect even without the internet and even backgrounding. Something like really bad practice.

Actually that’s what we recommend in general. Our preferred networking APIs both support a “wait for connectivity” option, where you start the connection and it’ll either connect immediately or, if that’s not possible, sit there waiting for connectivity to change and retrying each time. That handles situations like this perfectly (as long as your app doesn’t get suspended).

Specifically:

If you’re not using one of our preferred networking APIs, things get more complex.

So, what networking API are you using?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Internet state changes with code executing in the background
 
 
Q