Pawan Pillai
Pawan Pillai

Reputation: 2065

Xamarin iOS: How can one open SFSafariViewController using NSUrlSessionMultipathServiceType.Handover

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.

Upvotes: 0

Views: 682

Answers (0)

Related Questions