I'm developing Windows Phone 8 application. In this application, I have to connect to the server to get the data.
So Before connecting to the server, I want to check whether the internet connection available or not to the device. If the internet connection is available, then only I'll get the data from the server, Otherwise I'll show an error message.
Please tell me how to do this in Windows Phone 8.
NetworkInterface.GetIsNetworkAvailable()
returns the status of the NICs.
Depending on the status you can ask if the connectivity is established by using:
ConnectionProfile
-Class of Windows Phone 8.1 which uses the enum NetworkConnectivityLevel
:
This code should do the trick.
bool isConnected = NetworkInterface.GetIsNetworkAvailable();
if (isConnected)
{
ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
NetworkConnectivityLevel connection = InternetConnectionProfile.GetNetworkConnectivityLevel();
if (connection == NetworkConnectivityLevel.None || connection == NetworkConnectivityLevel.LocalAccess)
{
isConnected = false;
}
}
if(!isConnected)
await new MessageDialog("No internet connection is avaliable. The full functionality of the app isn't avaliable.").ShowAsync();
public static bool checkNetworkConnection()
{
var ni = NetworkInterface.NetworkInterfaceType;
bool IsConnected = false;
if ((ni == NetworkInterfaceType.Wireless80211)|| (ni == NetworkInterfaceType.MobileBroadbandCdma)|| (ni == NetworkInterfaceType.MobileBroadbandGsm))
IsConnected= true;
else if (ni == NetworkInterfaceType.None)
IsConnected= false;
return IsConnected;
}
Call this function and check whether internet connection is available or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With