I'm developing a NetworkExtension. I use an NWUDPSession to maintain a connection (of sorts—UDP is connectionless) with a server. I watch the session's hasBetterPath property and construct an "upgrade" session when it's true. You can prompt this behavior by switching off WiFi. The problem is this: after WiFi is switched off, the new session's `viable` property never becomes true, and it ignores all datagrams written to it.
That's the specific problem I'm tracking at the moment, but it's symptomatic of a set of problems with NetworkExtensions and network interface changes, such as switching the WiFi off.
- `NSURLSession.dataTaskWithRequest()` when called from the NetworkExtension times out after a long delay.
- POSIX UDP sockets created within the NetworkExtension successfully `connect()`, but `send()` fails with "Cannot allocate memory."
- As of iOS 10.0.2, the WiFi icon in the status bar remains gray after switching off the WiFi button in Settings. WiFi really does seem to be off and data from apps are routed through the WWAN device. The icon will remain in this state until the NetworkExtension is disabled (i.e. turn off the VPN switch in Settings) or WiFi is switched back on.
In general it appears that switching off WiFi on an iPhone causes NetworkExtensions to lose the ability to talk over any network interface until WiFi is switched on again. Is this the intended behavior? If not, how can we "upgrade" an NWUDPSession such that it becomes `viable` again? If so, how should NetworkExtensions continue operating on a WWAN context?
Significantly, if the NetworkExtension starts in a WWAN context (no WiFi), it communicates happily across cellular. If you then switch WiFi *on*, the NetworkExtension continues to communicate happily with no interruption. But if you then switch WiFi off again, the NetworkExtension fails in the ways described above, except that the WiFi status bar icon does successfully disappear in this context (even on iOS 10; this particular symptom never appears on iOS 9).
So my question: Am I doing something wrong? Or is it true, as it appears, that there is a set of operating system bugs surrounding NetworkExtensions and the loss of WiFi?