Asked by:
Check Internet connectivity

Question
-
User56953 posted
Hi, Can anybody suggest me how to check internet connectivity for both iOS and Android commonly using xamarin forms?
Thanks, sunil
Monday, June 30, 2014 10:26 AM
All replies
-
User43 posted
Hi @sunil.8167!
I'm not aware of a solution that works for both right out of the box. You could always use Xamarin.Form's DependencyService to create a simple solution.
On a platform-specific level, there is plenty of code you can use that others have already written that would be of help:
Our community has also created a project called Xamarin.Forms Labs, which also sports a Device service (currently in Beta) that can check internet connectivity.
Also, moving this thread to Xamarin.Forms forum, as this isn't a community topic.
Wednesday, July 2, 2014 2:48 AM -
User53502 posted
Just FYI - I had the same question and I went down the Xamarin.Forms Labs route based on the recommendation above.
I was wondering why it wasn't working, and then when I looked at the device-specific libraries, I noticed that they're coded to always return a null for the device.PhoneService.IsNetworkAvailable - so this is not implemented yet it seems!
Friday, July 11, 2014 2:39 PM -
User2496 posted
@Alex.9530 thanks for the notice, we have that methods implemented to Windows phone but stiil missing some properties on IOS and Android, we gonna fix that, you can follow the issue here:
https://github.com/XForms/Xamarin-Forms-Labs/issues/131
Friday, July 11, 2014 2:55 PM -
User12403 posted
I pushed in initial implementations for both iOS and Android. If you are working against the GitHub source any help in testing would be welcome. We probably have to implement a separate interface for the network as the phone service is only available on devices with call capabilities and not on tablets with WiFi.
Saturday, July 12, 2014 3:00 AM -
User91133 posted
Hi,
Do the current version of Xamarin.forms has support to check check internet connectivity?
Thanks, Sagar
Wednesday, December 24, 2014 6:17 AM -
User83484 posted
Hello,
anybody has worked out solution for this? I am interested in solution for Android, Windows Phone, iOS and both for phones and tablets.
Thanks, Piotr
Monday, January 5, 2015 11:41 PM -
User51805 posted
hello,
I think I found something that will do the trick if you are using MVVMCross, there is nice plugin here that does the job nicely:
https://github.com/aritchie/acrmvvmcross/tree/master/Acr.MvvmCross.Plugins.Network
Monday, March 30, 2015 3:20 PM -
User94258 posted
https://github.com/jamesmontemagno/Xamarin.Plugins might be one way to go.
Friday, July 3, 2015 6:51 PM -
User89714 posted
Be aware that solutions that rely on pinging a remote device may be unreliable if the remote device is configured to not respond to pings, or if firewalls block ICMP messages used by ping.
Saturday, July 4, 2015 2:21 PM -
User188006 posted
Hi All Used this Link for making Dependency service regarding above explained problem http://www.codeproject.com/Tips/870548/Xamarin-forms-Check-network-connectivity-in-iOS-an for this Dependency service need to be created
Friday, January 15, 2016 9:16 AM -
User188006 posted
and for Windows platform use this
public static bool getIsInternetAccessAvailable() { switch(NetworkInformation.GetInternetConnectionProfile().GetNetworkConnectivityLevel()) { case NetworkConnectivityLevel.InternetAccess: return true; default: return false; } }
Friday, January 15, 2016 9:18 AM -
User74071 posted
Here is an easy way without any plugins.
public bool CheckInternetConnection () { string CheckUrl = "http://google.com"; try { HttpWebRequest iNetRequest = (HttpWebRequest)WebRequest.Create (CheckUrl); iNetRequest.Timeout = 5000; WebResponse iNetResponse = iNetRequest.GetResponse (); // Console.WriteLine ("...connection established..." + iNetRequest.ToString ()); iNetResponse.Close (); return true; } catch (WebException ex) { // Console.WriteLine (".....no connection..." + ex.ToString ()); return false; } }
returns true, if internet connection was established and false, if not. Works for me! :smile:
Friday, January 15, 2016 12:01 PM -
User217924 posted
iNetRequest.GetResponse (); here "GetResponse()" is error not find properly.
Tuesday, April 26, 2016 5:27 AM -
User217924 posted
*Hi, *
Can u help me regarding network connectivity issue in windows phone xamarin app.
bool isConnected = NetworkInterface.GetIsNetworkAvailable(); this code always return TRUE when internet is connected or disconnect both time bool isConnected = NetworkInterface.GetIsNetworkAvailable(); these returns TRUE.
any else condition where bool isConnected = NetworkInterface.GetIsNetworkAvailable(); this condition will return FALSE then help regarding connectivity manager using in Android.
Please check attached screenshots.
Friday, April 29, 2016 10:38 AM -
User74071 posted
Hi @BharatJogarana
my sample above is a synchronous method that works in a Shared Project on iOS and Android.
For PCL projects and Windows Phone you need to use asynchronous methods. You could find a sample in Charles Petzold's BookPreview Chapter 20 Page 677.
Look Here .
Sunday, May 1, 2016 8:33 AM -
User85524 posted
`private bool isInternetAviable() {
try { Ping myPing = new Ping(); String host = "google.com"; byte[] buffer = new byte[32]; int timeout = 1000; PingOptions pingOptions = new PingOptions(); PingReply reply = myPing.Send(host, timeout, buffer, pingOptions); return true; } catch (Exception) { return false; } }
}`
Wednesday, July 6, 2016 9:28 PM -
User2112 posted
@giorgilekveishvili Try that on an actual device that is connected to mobile data not WIFI with no data bundles loaded and also no Airtime/money on your account and the Ping still returns true, not false as it should.
Android... I would recommend using:
public bool isOnline() { Runtime runtime = Runtime.GetRuntime(); try { Java.Lang.Process ipProcess = runtime.Exec("/system/bin/ping -c 1 8.8.8.8"); int exitValue = ipProcess.WaitFor(); return (exitValue == 0); } catch (Java.IO.IOException e) { e.PrintStackTrace(); } catch (InterruptedException e) { e.PrintStackTrace(); } return false; }
I found these results more accurate.
Permission Required:
<uses-permission android:name="android.permission.INTERNET" />
Source: http://stackoverflow.com/a/27312494
Friday, August 19, 2016 11:40 AM -
User251387 posted
This returns a bool:
return System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
Monday, August 22, 2016 5:59 PM -
User89714 posted
@ChristophThiel @giorgilekveishvili - If you're planning on having users in China, be aware that the Great Firewall of China may block access to Google. You may want to use an alternative if you believe your user is based in mainland China.
@giorgilekveishvili - As per comment earlier on thread, relying on ping can cause issues, as a result of firewalls not allowing some ICMP stuff through, or as a result of servers disabling ping.
Monday, August 22, 2016 6:18 PM -
User260731 posted
@MattFedorovich It's does not seems to be working. I've used Flight mode.
Monday, December 26, 2016 1:47 PM -
User308406 posted
Hi @Sunil,
check this out:
public bool CheckInternetConnection () { string CheckUrl = "https://www.google.co.in"; try{ HttpWebRequest iNetRequest = (HttpWebRequest)WebRequest.Create (CheckUrl); iNetRequest.Timeout = 5000; WebResponse iNetResponse = iNetRequest.GetResponse (); iNetResponse.Close (); return true; } catch (WebException ex) { return false; } }
Saturday, April 1, 2017 7:40 AM -
User243044 posted
Dependency Services is best for these type of issue, please check bellow link : https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/dependency-service/battery-info/
Tuesday, April 4, 2017 5:03 AM -
User222315 posted
@ChristophThiel said: Here is an easy way without any plugins.
public bool CheckInternetConnection () { string CheckUrl = "http://google.com"; try { HttpWebRequest iNetRequest = (HttpWebRequest)WebRequest.Create (CheckUrl); iNetRequest.Timeout = 5000; WebResponse iNetResponse = iNetRequest.GetResponse (); // Console.WriteLine ("...connection established..." + iNetRequest.ToString ()); iNetResponse.Close (); return true; } catch (WebException ex) { // Console.WriteLine (".....no connection..." + ex.ToString ()); return false; } }
returns true, if internet connection was established and false, if not. Works for me! :smile:
GetReponse not available in PCL
Saturday, April 22, 2017 10:57 AM -
User74071 posted
@Windyhen In PCL you need to use async methods
Saturday, April 22, 2017 3:18 PM -
User313001 posted
@Christoph Thiel thnks :smile:
Tuesday, August 22, 2017 11:52 AM -
User355485 posted
Full guide here :smile: https://youtu.be/Ck9tqaU0DhE
Monday, January 15, 2018 4:38 PM -
User344289 posted
Following instructions from the above members. Here's what I did so that the app asynchronously keeps checking if the internet is present every five seconds.
`async void CheckInternetConnectionASync() { while (true) { string CheckUrl = "http://google.com";
try { HttpWebRequest iNetRequest = (HttpWebRequest)WebRequest.Create(CheckUrl); iNetRequest.ContinueTimeout = 5000; WebResponse iNetResponse = await iNetRequest.GetResponseAsync(); iNetResponse.Dispose(); log.i("Internet connection present."); } catch (WebException ex) { log.e("CheckInternetConnectionASync: No internet connection", ex); await DisplayAlert("No Internet Connection", "Device seems to have lost internet connection." + " Please ensure the tablet has internet connectivity.", "OK"); } await Task.Delay(5000); } }`
Thursday, January 24, 2019 9:02 PM -
User89714 posted
@BoomShankar said: Following instructions from the above members. Here's what I did so that the app asynchronously keeps checking if the internet is present every five seconds.
Won't work in mainland China whilst the Great Firewall blocks access to Google. If using mobile data, will be clocking up charges. Not being done on a worker thread, so will cause lag on UI thread. Are you really sure you want to do DisplayAlert every 5 seconds (assuming user presses OK) if connecting to URL fails?
Thursday, January 24, 2019 9:13 PM -
User344289 posted
@JohnHardman I removed chunks of code which was specific to the logic I was working with. But this shortened version would do the trick and good call on the DisplayAlert, it shouldn't be there if not handled properly.
Also, Google could be replaced with something similar over there.
Thursday, January 24, 2019 9:26 PM -
User268511 posted
Use the Xamarin.Essentials package for this:
As I cannot post links yet, search for: xamarin essentials connectivity
Monday, October 28, 2019 1:22 PM