Network type in Xamarin.iOS

2019-02-27 09:50发布

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

2条回答
趁早两清
2楼-- · 2019-02-27 10:14

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

查看更多
forever°为你锁心
3楼-- · 2019-02-27 10:24

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();
}
查看更多
登录 后发表回答