1

I would like to check if mobile data is available (the app has access, it's switched on in settings, aeroplane mode isn't on and there is a valid connection).

I want to know this even when an active wifi connection is established. So I can't use any get the current connection as it won't be the current one, WiFi would be.

I have tried looking into NWPath and NWPathMonitor but got a bit confused by it all.

Thanks, if I need to add any more information just drop a comment and I will update this or reply to you.

Samuel James
  • 149
  • 2
  • 9
  • there are many, many existing questions on this subject. https://www.google.com/search?q=ios+api+mobile+data+enabled+site:stackoverflow.com – Jason Apr 06 '22 at 14:30
  • @Jason I am yet to find an answer though – Samuel James Apr 06 '22 at 14:33
  • the first result: "There is no available api's by which the app can query whether mobile data is enabled." – Jason Apr 06 '22 at 14:45
  • I got that same answer when talking about getting cell and wifi signals but managed to get that after converting someones SWIFT code – Samuel James Apr 07 '22 at 08:59

2 Answers2

2

Since you have a Tag with Xamarin, just use the API from Essentials. For the connectivity-checker see the docs.

Here is a pretty perfekt blogpost from "James Montemagno"

A sneak preview of the Post:

var current = Connectivity.NetworkAccess;

switch(current)
{
  case NetworkAccess.Internet:
    // Connected to internet
    break;
  case NetworkAccess.Local:
    // Only local network access
    break;
  case NetworkAccess.ConstrainedInternet:
    // Connected, but limited internet access such as behind a network login page
    break;
  case NetworkAccess.None:
    // No internet available
    break;
  case NetworkAccess.Unknown:
    // Internet access is unknown
    break;
}

Edit due to the comment from @Samuel James:

Maybe this helps?

Check what type of activ connection (according docs from Mircosoft):

 var profiles = Connectivity.ConnectionProfiles;
 if (profiles.Contains(ConnectionProfile.Cellular))
 {
   // Active mobile/cellular data connection.
 }
    
public enum ConnectionProfile
{
    /// <summary>Other unknown type of connection.</summary>
    Unknown,
    /// <summary>The bluetooth data connection.</summary>
    Bluetooth,
    /// <summary>The mobile/cellular data connection.</summary>
    Cellular,
    /// <summary>The ethernet data connection.</summary>
    Ethernet,
    /// <summary>The WiFi data connection.</summary>
    WiFi
}
Vs Ms
  • 101
  • 4
  • 1
    This doesn't show me if mobile data is available. This will show me if I am connected to the internet. I need to be able to distinguish while I am on wifi if mobile data is available to connect to. Not if I have an internet connection – Samuel James Apr 07 '22 at 08:20
  • @SamuelJames - maybe the edit helps? – Vs Ms Apr 07 '22 at 09:45
  • 1
    Thank you, I will give that a go and see if does what I want it to do and let you know :D – Samuel James Apr 07 '22 at 09:46
  • So on Android, it gives you all available but on iOS only currently connected. – Samuel James Apr 08 '22 at 10:40
0

See my answer here ,we can use CoreTelephony framework to get what the specific type of cellular is .

Try the following code


public ConnectivityTest()
{
    // Register for connectivity changes, be sure to unsubscribe when finished
    Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;
}

string result;
void Connectivity_ConnectivityChanged(object sender, ConnectivityChangedEventArgs e)
{
    var access = e.NetworkAccess;
    var profiles = e.ConnectionProfiles;
    if (Connectivity.NetworkAccess != NetworkAccess.Internet)
    {
        result = "NoNetworkAccess";                           
    }
    else if (profiles.Contains(ConnectionProfile.Cellular))
    {               
        CTTelephonyNetworkInfo networkInfo = new CTTelephonyNetworkInfo();
        string carrierTypeName = networkInfo.ServiceCurrentRadioAccessTechnology.Values[0];
        if (carrierTypeName == null) result = "unknown";
        else if (carrierTypeName == CTRadioAccessTechnology.GPRS ||
                carrierTypeName == CTRadioAccessTechnology.Edge ||
                carrierTypeName == CTRadioAccessTechnology.CDMA1x)
                result = "2G";
        else if (carrierTypeName == CTRadioAccessTechnology.WCDMA ||
                carrierTypeName == CTRadioAccessTechnology.HSDPA ||
                carrierTypeName == CTRadioAccessTechnology.HSUPA ||
                carrierTypeName == CTRadioAccessTechnology.CDMAEVDORev0 ||
                carrierTypeName == CTRadioAccessTechnology.CDMAEVDORevA ||
                carrierTypeName == CTRadioAccessTechnology.CDMAEVDORevB ||
                carrierTypeName == CTRadioAccessTechnology.EHRPD)
                result = "3G";
        else if (carrierTypeName == CTRadioAccessTechnology.LTE)
                result = "4G";
        else result = "5G";
    }                   
}

Refer to

https://stackoverflow.com/a/61327753/8187800

ColeX
  • 14,062
  • 5
  • 43
  • 240
  • If you turn off mobile data then it will still show unfortunately – Samuel James Apr 08 '22 at 10:45
  • You can get the current network status in event `ConnectivityChanged ` , and store the result into a global variable , please check my update answer. – ColeX Apr 11 '22 at 01:56
  • Hi , does it work or not? – ColeX Apr 14 '22 at 03:37
  • No it doesn't "profiles.Contains(ConnectionProfile.Cellular)" will only return true if the current connection is cellular not if there is an available one. i.e you're connected to wifi but mobile data is on in the background (this will return false) – Samuel James Apr 14 '22 at 11:00
  • Change `profiles.Contains(ConnectionProfile.Cellular)` to `profiles.Contains(ConnectionProfile.Cellular) || profiles.Contains(ConnectionProfile.WiFi)` , does it help? – ColeX Apr 15 '22 at 07:40