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/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:
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();
}
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