Search code examples
c#-4.0xamarin.formsinternet-connection

Internet Connectivity Listener in Xamarin.Forms


I am new in Xamarin.Forms and I want to check internet connectivity status in iOS and Android app. In fact using CrossConnectivity Plugins I am able to check internet connectivity successfully but It is not working as Listener. For example, When I open my app and internet connection is not there then it shows me message that "No internet connection" and now if I ON my mobile data then also it shows me same message. I am using below code for this:

string isConnected=CrossConnectivity.Current.IsConnected?"Connected":"No Connection";

My app is not able to listen the changing status of internet connectivity in middle something.


Solution

  • Using the plugin CrossConnectivity, you need to listen to changes via the event ConnectivityChanged, so in your page, or your App class, add this code to write an event handler:

    CrossConnectivity.Current.ConnectivityChanged += (sender, args) =>
     {
       //your implementation
      this.DisplayAlert("Connectivity Changed", "IsConnected: " + args.IsConnected.ToString(), "OK");
     };