Asked by:
I am a complete noob to xamarin.How to force my app to use only cellular data ?

Question
-
User399300 posted
I have made an app that seemed perfect till I realized it crashes using cellular data but NOT using wi fi .Before any thing else I want to know how to tell my app to only use cellular data .Any answer or clue would be greatly appreciated.
Wednesday, February 3, 2021 7:57 PM
All replies
-
User379860 posted
For android, you can use following code to
ForceWifiOverCellular
orForceCellularOverWifi
``` using Android.Content; using Android.Net; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace SumOfEntrys.Droid { class ForceNetworkType { public static Context _context = Android.App.Application.Context;
/// <summary> /// Forces the wifi over cellular /// </summary> public static void ForceWifiOverCellular() { ConnectivityManager connection_manager = (ConnectivityManager)_context.GetSystemService(Context.ConnectivityService); NetworkRequest.Builder request = new NetworkRequest.Builder(); request.AddTransportType(TransportType.Wifi); var callback = new ConnectivityManager.NetworkCallback(); connection_manager.RegisterNetworkCallback(request.Build(), new CustomNetworkAvailableCallBack()); } /// <summary> /// Forces the cellular over wifi. /// </summary> public static void ForceCellularOverWifi() { ConnectivityManager connection_manager = (ConnectivityManager)_context.GetSystemService(Context.ConnectivityService); NetworkRequest.Builder request = new NetworkRequest.Builder(); request.AddTransportType(TransportType.Cellular); connection_manager.RegisterNetworkCallback(request.Build(), new CustomNetworkAvailableCallBack()); } } /// <summary> /// Custom network available call back. /// </summary> public class CustomNetworkAvailableCallBack : ConnectivityManager.NetworkCallback { public static Context _context = Android.App.Application.Context; ConnectivityManager connection_manager = (ConnectivityManager)_context.GetSystemService(Context.ConnectivityService); public override void OnAvailable(Network network) { //ConnectivityManager.SetProcessDefaultNetwork(network); //deprecated (but works even in Android P) connection_manager.BindProcessToNetwork(network); //this works in Android P } }
} ```
For xamarin.form, you can create a dependence service to achieve it.
``` public interface IChangeNetworkSettings { void ForceWifiOverCellular();
void ForceCellularOverWifi(); }
```
In the android platform, you can achieve the interface.
``` [assembly: Dependency(typeof(ChangeNetworkSettingsService))] namespace SumOfEntrys.Droid { public class ChangeNetworkSettingsService : IChangeNetworkSettings { public void ForceCellularOverWifi() { //throw new NotImplementedException(); ForceNetworkType.ForceCellularOverWifi(); }
public void ForceWifiOverCellular() { // throw new NotImplementedException(); ForceNetworkType.ForceWifiOverCellular(); } }
} ```
But for iOS. it cannot be achieved, your app is sandboxed and can not access the Wifi manager's keychain, I cannot find a way to access it.
Xamarin forums are migrating to a new home on Microsoft Q&A! We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A! For more information, please refer to this sticky post.
Thursday, February 4, 2021 3:14 AM -
User379860 posted
Are there any update for this issue, please reply is helpful, please click the
Yes
tab under the helpful answer.Friday, March 5, 2021 8:34 AM -
User399300 posted
Sorry for the late response .I had moved to a different project .I will definitely attempt it. Thank you very much for taking the time and effort to address the issue with such details.
Friday, March 5, 2021 12:35 PM -
User379860 posted
You are welcome.
Xamarin Community Forums is moving to Microsoft Q&A! For new questions: we invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A! For existing questions: follow up is still in service until April 15th, 2021. For more information, please refer to this sticky post.
Monday, March 8, 2021 7:43 AM