14

How can I check internet access with MonoTouch? I don't care if the internet comes from a WIFI or the Cell Network, all I care is whether there is internet access or not.

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
Jonas Stawski
  • 6,682
  • 6
  • 61
  • 106
  • 1
    The above link to the Reachabiity class has been moved to the following: https://github.com/xamarin/monotouch-samples/blob/master/ReachabilitySample/reachability.cs –  Sep 13 '11 at 06:21

1 Answers1

20

Using Miguel's Reachability class (found here: https://github.com/xamarin/monotouch-samples/blob/master/ReachabilitySample/reachability.cs) you can just call it like this:

if(!Reachability.IsHostReachable("http://google.com")) {
    // Put alternative content/message here
}
else
{
    // Put Internet Required Code here
}

Hope this helps,

ChrisNTR

dalexsoto
  • 3,412
  • 1
  • 27
  • 43
chrisntr
  • 2,208
  • 20
  • 28
  • Thanks, chisntr. So far this has worked on the simulator. Will update if i run into problems with the real hardware. – Jonas Stawski Dec 29 '09 at 05:34
  • I've got an app up on the AppStore using the same code and seems to work on all devices, if you run in to any problems then let me know. – chrisntr Dec 30 '09 at 15:22
  • I've updated the link - but now the sample code won't run as the class has gone static (sorry!) - won't be hard for people to work out though! – Stuart Mar 22 '12 at 11:13
  • 1
    @chrisntr: The class seems to have issues when calling its InternetConnectionStatus(). In airplane/flight mode, it will always return Wifi – skarmats May 15 '12 at 06:11
  • Hi ChrisNTR: I believe static classes can't be instantiateed, so I: - removed the instantiation - replaced the if statement with: **if(!Reachability.IsHostReachable("www.bestwebbuys.com"))** It now works for me... – SpokaneDude Dec 02 '10 at 22:21
  • @skarmats : The issue with InternetConnectionStatus() should now be fixed in github. – Tom Opgenorth Jul 02 '12 at 20:06
  • 3
    The method IsHostReachable requires a host, so the example code should be: `if(!Reachability.IsHostReachable("google.com"))`, without the http prefix. – Arno van der Meer Jan 21 '13 at 10:40