Home > Mobile >  Xamarin iOS - Check if mobile data connection is available
Xamarin iOS - Check if mobile data connection is available

Time:04-08

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.

CodePudding user response:

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
}

CodePudding user response:

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

Try the following code

string getConnectionType()
{
    var profiles = Connectivity.ConnectionProfiles;
    if (profiles.Contains(ConnectionProfile.Cellular))
    {
        CTTelephonyNetworkInfo networkInfo = new CTTelephonyNetworkInfo();
        string carrierTypeName = networkInfo.ServiceCurrentRadioAccessTechnology.Values[0];
        if (carrierTypeName == null) return "unknown";

        if (carrierTypeName == CTRadioAccessTechnology.GPRS ||
            carrierTypeName == CTRadioAccessTechnology.Edge ||
            carrierTypeName == CTRadioAccessTechnology.CDMA1x)
            return "2G";
        if (carrierTypeName == CTRadioAccessTechnology.WCDMA ||
            carrierTypeName == CTRadioAccessTechnology.HSDPA ||
            carrierTypeName == CTRadioAccessTechnology.HSUPA ||
            carrierTypeName == CTRadioAccessTechnology.CDMAEVDORev0 ||
            carrierTypeName == CTRadioAccessTechnology.CDMAEVDORevA ||
            carrierTypeName == CTRadioAccessTechnology.CDMAEVDORevB ||
            carrierTypeName == CTRadioAccessTechnology.EHRPD)
            return "3G";
        if (carrierTypeName == CTRadioAccessTechnology.LTE)
            return "4G";

        return "5G";
    }
    return "unknown";
}

Refer to

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

  • Related