0

I have a multiplatform app in Xamarin which connects to the internet (using Phone's cellular network) for some purposes and then talk to a local WIFI network (without internet access ) for some other purposes. I want to do this network switch seamlessly from the user (no user interaction required).

For the Android part of my app, I was successful in implementing this using:

NetworkRequest.Builder request = new NetworkRequest.Builder();
request.AddTransportType(TransportType.Cellular);

then later:

ConnectivityManager.BindProcessToNetwork(network)

Full explanation provided by me here: How to connect to local network first instead of internet in C# Xamarin Android app?

But I am not able to understand how to achieve the same for iOS in the app. I have read the following links:

https://developer.apple.com/documentation/foundation/urlsessionconfiguration/improving_network_reliability_using_multipath_tcp

https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-walkthroughs/background-transfer-walkthrough

https://developer.apple.com/documentation/foundation/urlsessionconfiguration/multipathservicetype

And enabled Multipath TCP entitlement for my App.

Can someone guide me in the right direction as to how/where set the NSUrlSession object to NSUrlSessionMultipathServiceType.Handover in the whole iOS application lifecycle.

I know I am not able to provide much info, but any help in the right direction is much appreciated.

Update: After adding the following code as per @SushiHangover's answer

var config = NSUrlSessionConfiguration.DefaultSessionConfiguration;
config.MultipathServiceType = NSUrlSessionMultipathServiceType.Handover;

I am getting the following warning in Xamarin: enter image description here

Final Working Update: After lot of tries, I found out that for some reason, Xamarin was not properly mapping the Enum value to its number. So I had to manually set the Enum value and now the Handover (i.e. Multipath TCP) is working fine. Updated code:

public void GetDataFromInternet(string strURL)
{
    NSUrl url = new NSUrl(strURL);
    NSUrlRequest request = new NSUrlRequest(url);
    NSUrlSession session = null;
    NSUrlSessionConfiguration config = NSUrlSessionConfiguration.DefaultSessionConfiguration;
    //config.MultipathServiceType = NSUrlSessionMultipathServiceType.Handover;    //for some reason this does not work!!
    config.MultipathServiceType = (NSUrlSessionMultipathServiceType)2;            //but this works!!
    session = NSUrlSession.FromConfiguration(config);

    NSUrlSessionTask task = session.CreateDataTask(request, (data, response, error) => {
        Console.WriteLine(data);
    });
    task.Resume();
}
Pawan Pillai
  • 1,955
  • 5
  • 37
  • 64

1 Answers1

1

how/where set the NSUrlSession object to NSUrlSessionMultipathServiceType.Handover in the whole iOS application lifecycle.

You can change the default NSUrl session via the DefaultSessionConfiguration:

var config = NSUrlSessionConfiguration.DefaultSessionConfiguration;
config.MultipathServiceType = NSUrlSessionMultipathServiceType.Handover;

Note: Assuming you will want to do this as early in the app lifecycle as possible, i.e. in the AppDelegate.FinishedLaunching override.

P.S. This is not the same as what you are doing on the Android and does require that your server (and much more likely, router in front of the server(s)) is Multipath TCP enabled to handle the auto-switch between the incoming paths

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • Thanks for the suggestion, but its not working and I am getting a warning in Xamarin (and "Show Potential Fixes" does not show any suggestion). Please see the update above in my question. – Pawan Pillai Aug 14 '19 at 13:56
  • @PawanPillai iOS 11.0+ – SushiHangover Aug 14 '19 at 19:39
  • Yes, I am using it with iOS 12.4 SDK version. Still, it's giving that message. I have tried many other options, but nothing works. As a workaround, I also tried connecting to local wifi for some cases (rest works using cellular internet) using method like this: https://spin.atomicobject.com/2018/02/15/connecting-wifi-xamarin-forms/ but this always gives NEHotspotConfigurationErrorDomain Code=8 "internal error." I am unable to resolve this error also even though I tried lot of things based on Google. Please see if you can still guide in any direction. Thanks in advance. – Pawan Pillai Aug 15 '19 at 00:37
  • Finally I was able to fix it. For some reason Xamarin is not converting the ENUM value properly. See final update above on how it worked. But your solution helped. So marking is as the answer. Thanks a lot. – Pawan Pillai Aug 16 '19 at 20:20