Network type in Xamarin.iOS

2019-02-27 10:14发布

问题:

I am work in Xamarin. And now I want to get the current type of network in Xamarin.iOS .Such as 4G,3G,wifi...

I am new in Xamarin. Any suggestion would be useful

回答1:

To get connected network type use CTTelephonyNetworkInfo class like this

var telephonyInfo = new CTTelephonyNetworkInfo();
var networkType = telephonyInfo.CurrentRadioAccessTechnology;

Possibles values defined which you will get are as follows : CTRadioAccessTechnologyGPRS, CTRadioAccessTechnologyEdge ,CTRadioAccessTechnologyWCDMA , CTRadioAccessTechnologyLTE etc



回答2:

You can grab this Reachability class which comes from an iOS sample: https://github.com/xamarin/ios-samples/blob/master/ReachabilitySample/reachability.cs

Then you can get the connection status with:

var internetStatus = Reachability.InternetConnectionStatus();
switch(internetStatus)
{
    case NetworkStatus.ReachableViaCarrierDataNetwork:
        // do something on cellular network
        break;
    case NetworkStatus.ReachableViaWiFiNetwork:
        // do something on wifi network
        break;
}

You can get notified for changes with:

Reachability.ReachabilityChanged += OnReachabilityChanged;

private void OnReachabilityChanged(object sender, EventArgs args)
{
    internetStatus = Reachability.InternetConnectionStatus();
}