Skip to content Skip to sidebar Skip to footer

.net Or Xamarin Internet Availability Check When The WIFI Network Has No Internet Connection

i know its a big discussion how to check if there is a internet connection available for the device. I tried Ping, WebClient and HTTPClient. I also use Xamarin Essentials and the C

Solution 1:

Answer :

You can use DependencyServiceto implement it .Refer the following code.

in Forms ,create an interface

using System;
namespace app1
{
  public interface INetworkAvailable
  {

    bool IsNetworkAvailable();
  }
}

in iOS project

using System;
using Xamarin.Forms;
using Foundation;

[assembly: Dependency(typeof(IsNetworkAvailableImplement))]
namespace xxx.iOS
{
  public class IsNetworkAvailableImplement:INetworkAvailable
  {
    public IsNetworkAvailableImplement()
    {
    }

    bool INetworkAvailable.IsNetworkAvailable()
    {
        NSString urlString = new NSString("http://captive.apple.com");

        NSUrl url = new NSUrl(urlString);

        NSUrlRequest request = new NSUrlRequest(url, NSUrlRequestCachePolicy.ReloadIgnoringCacheData, 3);

        NSData data = NSUrlConnection.SendSynchronousRequest(request, out NSUrlResponse response, out NSError error);

        NSString result = NSString.FromData(data,NSStringEncoding.UTF8);

        if(result.Contains(new NSString("Success")))
        {
            return true;
        }

        else
        {
            return false;
        }

    }
  }
}

Don't forget to allow HTTP access .add the following code in info.plist

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict> 

in Android project

using System;
using Java.Lang;
using Xamarin.Forms;

[assembly: Dependency(typeof(IsNetworkAvailableImplement))]
namespace xxx.Droid
{
  public class IsNetworkAvailableImplement:INetworkAvailable
  {
    public IsNetworkAvailableImplement()
    {
    }

    public bool IsNetworkAvailable()
    {
        Runtime runtime = Runtime.GetRuntime();

        Process process = runtime.Exec("ping -c 3 www.google.com");

        int result = process.WaitFor();

        if(result==0)
        {
            return true;
        }

        else
        {
            return false;
        }
    }
  }
}

Now you can call it in forms ,just like

bool isAvailable= DependencyService.Get<INetworkAvailable>().IsNetworkAvailable();

if(isAvailable)
{
  Console.WriteLine("network is available");
}

else
{
  Console.WriteLine("network is unavailable");
} 

Solution 2:

Edited: Xamarin Essentials is the best solution as it offers more options, this is a sample of how to use it

  • after setup Xamarin Essential NuGet in your project

  • your code we be like the following

             if (Xamarin.Essentials.Connectivity.NetworkAccess == Xamarin.Essentials.NetworkAccess.Internet)
             {
                 //your Code if there is an internet
             }
             else
             {
                 //your Code if there isn't an internet
             }
    

look at this: https://github.com/xamarin/Essentials


Post a Comment for ".net Or Xamarin Internet Availability Check When The WIFI Network Has No Internet Connection"