Force HTTP calls go over WIFI network on Xamarin iOS

I have an app, developed in Xamarin Forms, which connects programmatically to a WIFI (with no internet access) exposed by an IoT device. This is the code I use to connect to the wifi network:

NEHotspotConfiguration configuration = new NEHotspotConfiguration("ssid");
Device.BeginInvokeOnMainThread(() =>
{
    NEHotspotConfigurationManager.SharedManager.ApplyConfiguration(configuration, (NSError error) =>
    {
        if (error != null)
        {
            if (error.LocalizedDescription == "already associated.")
            {
                // connected!
                CommunicateConnected();
            }
            else
            {
                // not connected
                CommunicateNotConnected();
            }
        }
        else
        {
            // connected!
            CommunicateNotConnected();
        }
    });
});

Once the connection is successfully established the app performs some HTTP calls to the IP of the IoT device.

Sometimes it happens that the HTTP calls go over the cellular data connection instead of WIFI connection (because the joined network has no internet connection?) and they fail. (Simply, I see that the 4G icon is still visible instead of the one representing the WIFI connection).

The switch between mobile connection and WIFI connection, in these cases, happens after some minutes. Is there a way to force the HTTP calls to go over the just joined network?

I have an app, developed in Xamarin Forms

I am going to approach this post as though this were a native app.

Regarding:

Once the connection is successfully established the app performs some HTTP calls to the IP of the IoT device. Sometimes it happens that the HTTP calls go over the cellular data connection instead of WIFI connection (because the joined network has no internet connection?) and they fail. Is there a way to force the HTTP calls to go over the just joined network?

Not exactly. There are things that you can do to improve the situation though. First, make sure that you give your app a bit of a delay, 3-5 seconds before trying to communicate with the IoT device. This gives the device time to establish an IP etc...

Next, if you were using URLSession you could set allowsCellularAccess to false for the URLRequest being used in the URLSessionDataTask and this would require the task to go over Wi-Fi.

Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com

HI, I have the problem that my IOT device don't work with iOS, in Android the connection works without problem but in iOS the same code you posted don't work.

I think it depends on some property missing in my Entitlements file.. Could be possible? Can you share your Entitlements?

Thanks

There is a sample project here that shows the usage of the NEHotspotConfiguration API along with it's entitlements.

Force HTTP calls go over WIFI network on Xamarin iOS
 
 
Q