3

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.

wonea
  • 4,783
  • 17
  • 86
  • 139
Dipak
  • 1,199
  • 4
  • 21
  • 43
  • I did it specifically per plateform if you are interested i can provide an answer otherwise i dont know if it exists in XF.. – Mr.Koçak Nov 07 '16 at 08:39
  • Sure. I can use dependency service. Please give me a platform specific solution for the same. – Dipak Nov 07 '16 at 09:26

2 Answers2

4

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");
 };
Hicham
  • 720
  • 6
  • 9
  • Is this event will call for every single event when connectivity changed? – Dipak Nov 07 '16 at 14:42
  • Where should I declare this event so that it will fire when Connectivity is Changed? – Dipak Nov 07 '16 at 14:47
  • It depends on your needs, you can call it in OnAppearing method of your page if you need to listen to changes only while your page is visible. – Hicham Nov 07 '16 at 16:33
  • Yes. Thank you. Let me check. – Dipak Nov 08 '16 at 04:39
  • On iOS, for disconnecting connection it shows "IsConnected: true" – Dipak Nov 08 '16 at 09:38
  • I just checked with my device, and it shows the correct value, are you testing with simulator or real device? Anyway, if you found that there is a bug with the plugin, you should inform his maintainer in his github page to fix it. – Hicham Nov 08 '16 at 10:05
2

I have the solution for Android but i haven't started working on ios part (better than nothing ;) First Create a broadcastReceiver

    public class Localize: BroadcastReceiver
    {
        public static Context context;

        public Localize(Context ctx)
        {
            context = ctx;
        }
        public override void OnReceive (Context context, Intent intent)
        {
            isNetworkAvailable (context);
        }

    public void isNetworkAvailable(Context context)
    {
        Boolean state = false;
        ConnectivityManager connectivity = (ConnectivityManager) 
            context.GetSystemService(Context.ConnectivityService);
        if (connectivity != null) 
        {
            NetworkInfo[] info = connectivity.GetAllNetworkInfo();
            foreach (NetworkInfo nwork in info) 
            {
                if (nwork.GetState () == NetworkInfo.State.Connected) {

                    ConnectionDetected();//Execute your fonction here
                    break;

                }
            }
        }
    }

Then register your broadcastreceiver with intent in your activity (in MainActivity for example)

IntentFilter filter = new IntentFilter(ConnectivityManager.ConnectivityAction);
receiver = new Localize(this);
RegisterReceiver(receiver, filter);

This should work as long as your application is running.. If you want a service that runs even if your App is killed you should create a service and then register broadcastReceiver in your service..

CrossConnectivity.Current.IsReachable("localhost");

this also works if you download package. I haven't tested it thoroughly

Mr.Koçak
  • 313
  • 4
  • 9