0

I have a Xamarin application that primarily connects with local WIFI router (without internet access) to communicate with some local hardware devices. And for some features, the app uses NSUrlSessionMultipathServiceType.Handover (mobile's cellular network) to connect to internet-based APIs. All this works perfectly using the code given below:

/// <summary>
        /// Downloads file from dropbox
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public Task<string> GetFile(string filepath)
        {
            TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
            NSUrl url = NSUrl.FromString(AppConfig.DropboxFileDownloadBaseUrl + AppConfig.DropboxFileDownloadEndpointUrl);

            try
            {
                NSUrlRequest req = new NSUrlRequest(url);
                var BearerToken = "Bearer " + AppConfig.DropboxAccessToken;
                var DropboxPathHeader = "{\"path\":\"" + filepath + "\"}";

                if (!string.IsNullOrEmpty(BearerToken))
                {
                    NSMutableUrlRequest mutableRequest = new NSMutableUrlRequest(url);

                    try
                    {
                        NSMutableDictionary dictionary = new NSMutableDictionary();
                        dictionary.Add(new NSString("Authorization"), new NSString(BearerToken));
                        dictionary.Add(new NSString("Dropbox-API-Arg"), new NSString(DropboxPathHeader));
                        mutableRequest.Headers = dictionary;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                    mutableRequest.HttpMethod = "POST";
                    req = (NSUrlRequest)mutableRequest.Copy();
                }



                NSUrlSession session = null;
                NSUrlSessionConfiguration myConfig = NSUrlSessionConfiguration.DefaultSessionConfiguration;
                myConfig.MultipathServiceType = NSUrlSessionMultipathServiceType.Handover;

                session = NSUrlSession.FromConfiguration(myConfig);
                NSUrlSessionTask task = session.CreateDataTask(req, (data, response, error) =>
                {
                    if (response is NSHttpUrlResponse)
                    {
                        var objNSHttpUrlResponse = response as NSHttpUrlResponse;
                        Console.WriteLine(objNSHttpUrlResponse.StatusCode);

                        if (objNSHttpUrlResponse.StatusCode == 200)
                        {
                            //Console.WriteLine(data);
                            byte[] dataBytes = new byte[data.Length];
                            Marshal.Copy(data.Bytes, dataBytes, 0, Convert.ToInt32(data.Length));

                            string enc_json = Encoding.Unicode.GetString(dataBytes);


                            //tell the TaskCompletionSource that we are done here:
                            tcs.TrySetResult(enc_json);
                        }
                        else
                        {
                            //tell the TaskCompletionSource that we are done here:
                            tcs.TrySetResult(null);
                        }
                    }
                    else
                    {
                        //tell the TaskCompletionSource that we are done here:
                        tcs.TrySetResult(null);
                    }


                });

                task.Resume();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                //tell the TaskCompletionSource that we are done here:
                tcs.TrySetResult(null);
            }

            return tcs.Task;
        }

But now I have a requirement to authenticate users using Dropbox OAuth API using SFSafariViewController. I know how to use SFSafariViewController when I am on internet-enabled WIFI or Cellular network.

But I am not able to find a way to automatically switch to iPhone's cellular network when iPhone is connected to "non-internet-enabled" WIFI router when I want to open some OAuth URL (OAuth URL example below) or any URL (like https://www.google.com) using SFSafariViewController.

OAuth URL: https://www.dropbox.com/oauth2/authorize?client_id=DROPBOX_KEY&response_type=code&redirect_uri=REDIRECT_URL

Please advise. Thanks.

Pawan Pillai
  • 1,955
  • 5
  • 37
  • 64
  • Hi , do you mean can not automatically connecting to cellular network when wifi is not available ? I am not sure [ASWebAuthenticationSession](https://developer.apple.com/documentation/authenticationservices/aswebauthenticationsession?language=objc) whether be helpful for you to find the solution. – Junior Jiang Oct 23 '19 at 02:29
  • Thanks for your comment but I don't think ASWebAuthenticationSession will help me. My app primarily runs on a local wifi router (which does "not" have internet access). So for certain internet-based things, I use Multipath TCP (in which iOS automatically switches to my cellular LTE connection) as given in the code above. This works fine when all I need to do is to call some APIs using NSUrlSessionTask. But now I need to open some website using SFSafariViewController. So for this, I am not able to find a way to enable Multipath TCP. Hope this comment puts some more light on my query. – Pawan Pillai Oct 23 '19 at 03:35
  • So, basically I cannot ask user to manually turn off Wifi. I want iOS to automatically switch to Cellular (using Multipath TCP) when I want to use SFSafariViewController. Once SFSafariViewController's work is done, iOS should go back to Wifi network. Again, all this works perfectly using the above code. I just want to know if something similar is possible with SFSafariViewController. Thanks. – Pawan Pillai Oct 23 '19 at 03:37
  • Okey , got it . If have solution will update here . – Junior Jiang Oct 23 '19 at 05:50
  • Thanks, I am also trying different approaches but lack of documentation with Multipath TCP is hurting me. In Android, we can easily switch the network interface to make all calls from cellular or wifi. So this workflow works easily in Android. In IOS, I am so far not able to find a way to switch network interface. Only NSUrlSessionMultipathServiceType.Handover is able to help with partial network switch but only for direct URL calls (and not through SFSafariViewController so far). Hoping for a solution. – Pawan Pillai Oct 23 '19 at 14:08
  • It seems Apple not open the interface to switch network for App . Before finding the solution , we just can give a setting url to guide customer to setting page to switch them manually. >. – Junior Jiang Oct 24 '19 at 02:02
  • Later, I found a [discussion](https://discussions.apple.com/thread/3703817?tstart=0) about route ip not setting can make it work .But not sure here can work for SFSafariViewController . – Junior Jiang Oct 24 '19 at 08:05
  • Yea, I think this is a setting on the actual iPhone. I would like to do this switch programmatically (like how it works with Handover). I will log a ticket with Apple's technical support. I will keep you posted. Thanks. – Pawan Pillai Oct 24 '19 at 13:45
  • 1
    Great , I also send a message to Apple , if any good news also will update here. – Junior Jiang Oct 25 '19 at 07:38
  • As per Apple, it's not possible currently. – Pawan Pillai May 28 '21 at 16:22

0 Answers0