IP Address change detection in an App

Hello,


I am writing video+audioapp, and wanted to handle IP address change event when the phone switches from one wifi network to other when an app is in a live video+audio call.


I am using Apple's sample code for Reachability (https://developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html) to detect network change events. When cellular data is disabled, if I switch from one ipv4 wifi network to another ipv4 wifi network, the reachability callback does not get fired. Please note that app is accessing microphone and playing audio which keeps app active in the background when I am switching network from iPhone's general settings.


(I experimented the same scenario on iPhone's Facetime app, and handover worked as expected after IP address changed.)


Any advice or suggestion to detect IP address changes reliably will be greatly appreciated.


Thank you,

Piyush Tank

Using 'Reachability', your app can only detect if a connection exists. And it doesn't actively poll and/or manage a connection for you. Are you polling again after changing networks?

I am using Apple's sample code for Reachability … to detect network change events.

Reachability is the right direction, but that sample won’t help because it doesn’t demonstrate the API you need, namely,

SCNetworkReachabilityCreateWithAddressPair
. The trick here is:
  1. Set up your call

  2. Once it’s in place, get the source and destination addresses associated with the call (if you’re using BSD Sockets, get these via

    getsockname
    and
    getpeername
    )
  3. Pass those addresses to

    SCNetworkReachabilityCreateWithAddressPair
  4. Schedule that for async notifications

  5. If the device’s IP address changes in such a way as to ‘break’ your connection, reachability will tell you

There’s some subtlety to the last point. Consider this sequence:

  1. You set up a call over WWAN

  2. The user enters their home and the device joins the home Wi-Fi

At this point the default route switches to Wi-Fi but connections over WWAN are still liable (due to iOS’s scoped routing feature) and thus your reachability notification won’t fire. That’s probably what you want in this case, but if not you can use a second reachability query to look for WWAN to Wi-Fi transitions and manually move the call over to Wi-Fi in that case.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi Quinn,


I used `SCNetworkReachabilityCreateWithAddressPair` in reachability module, and now our app is consistently getting "network lost" event user switches the wifi on iPhone settings.


We want to hand over the video call to new IP address if a user switches wifi during a live video call. For that, my next question is -

Do you have any recommendation on how our app should detect the new IP address?

Should our app poll for the new IP address after receiving the "network lost" event (caused by changing the wifi networks)? If yes, is there any recommended way for polling the new IP address.


Thank you very much for your help!


Piyush

IP addresses don’t exist in a vacuum; an IP address is only meaningful if you can communicate over it. To that end my standard recommendation is:

  1. Try reconnecting immediately.

  2. If that works, you’re done, and you can monitor the local and remote addresses of the connection via

    SCNetworkReachabilityCreateWithAddressPair
    .
  3. If that doesn’t work, wait until it’s a good time to retry.

  4. Go back to step 1.

Note You can use standard reachability as part of your retry scheduling code in step 3.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I've tried to use SCNetworkReachabilityCreateWithAddressPair to follow the connectivity of just established BSD socket. At least on iOS 17.5.1 it never works. It does report 0 as reachability flags for sockets which is connected and functional. I also unable to make it send me any callback when I actually change network interface and previous network route is not viable.

IP Address change detection in an App
 
 
Q