Skip to content

Instantly share code, notes, and snippets.

@redent
Last active November 2, 2017 11:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redent/9956103 to your computer and use it in GitHub Desktop.
Save redent/9956103 to your computer and use it in GitHub Desktop.
MonoTouch Reachability class
using System;
using System.Net;
using MonoTouch.SystemConfiguration;
using MonoTouch.CoreFoundation;
public enum NetworkStatus {
NotReachable,
ReachableViaCarrierDataNetwork,
ReachableViaWiFiNetwork
}
public static class Reachability {
public static string HostName = "www.google.com";
public static bool IsReachableWithoutRequiringConnection (NetworkReachabilityFlags flags)
{
// Is it reachable with the current network configuration?
bool isReachable = (flags & NetworkReachabilityFlags.Reachable) != 0;
// Do we need a connection to reach it?
bool noConnectionRequired = (flags & NetworkReachabilityFlags.ConnectionRequired) == 0;
// Since the network stack will automatically try to get the WAN up,
// probe that
if ((flags & NetworkReachabilityFlags.IsWWAN) != 0)
noConnectionRequired = true;
return isReachable && noConnectionRequired;
}
// Is the host reachable with the current network configuration
public static bool IsHostReachable (string host)
{
if (host == null || host.Length == 0)
return false;
using (var r = new NetworkReachability (host)){
NetworkReachabilityFlags flags;
if (r.TryGetFlags (out flags)){
return IsReachableWithoutRequiringConnection (flags);
}
}
return false;
}
//
// Raised every time there is an interesting reachable event,
// we do not even pass the info as to what changed, and
// we lump all three status we probe into one
//
public static event EventHandler ReachabilityChanged;
static void OnChange (NetworkReachabilityFlags flags)
{
var h = ReachabilityChanged;
if (h != null)
h (null, EventArgs.Empty);
}
//
// Returns true if it is possible to reach the AdHoc WiFi network
// and optionally provides extra network reachability flags as the
// out parameter
//
static NetworkReachability adHocWiFiNetworkReachability;
public static bool IsAdHocWiFiNetworkAvailable (out NetworkReachabilityFlags flags)
{
if (adHocWiFiNetworkReachability == null){
adHocWiFiNetworkReachability = new NetworkReachability (new IPAddress (new byte [] {169,254,0,0}));
adHocWiFiNetworkReachability.SetCallback (OnChange);
adHocWiFiNetworkReachability.Schedule (CFRunLoop.Current, CFRunLoop.ModeDefault);
}
if (!adHocWiFiNetworkReachability.TryGetFlags (out flags))
return false;
return IsReachableWithoutRequiringConnection (flags);
}
static NetworkReachability defaultRouteReachability;
static bool IsNetworkAvailable (out NetworkReachabilityFlags flags)
{
if (defaultRouteReachability == null){
defaultRouteReachability = new NetworkReachability (new IPAddress (0));
defaultRouteReachability.SetCallback (OnChange);
defaultRouteReachability.Schedule (CFRunLoop.Current, CFRunLoop.ModeDefault);
}
if (!defaultRouteReachability.TryGetFlags (out flags))
return false;
return IsReachableWithoutRequiringConnection (flags);
}
static NetworkReachability remoteHostReachability;
public static NetworkStatus RemoteHostStatus ()
{
NetworkReachabilityFlags flags;
bool reachable;
if (remoteHostReachability == null){
remoteHostReachability = new NetworkReachability (HostName);
// Need to probe before we queue, or we wont get any meaningful values
// this only happens when you create NetworkReachability from a hostname
reachable = remoteHostReachability.TryGetFlags (out flags);
remoteHostReachability.SetCallback (OnChange);
remoteHostReachability.Schedule (CFRunLoop.Current, CFRunLoop.ModeDefault);
} else
reachable = remoteHostReachability.TryGetFlags (out flags);
if (!reachable)
return NetworkStatus.NotReachable;
if (!IsReachableWithoutRequiringConnection (flags))
return NetworkStatus.NotReachable;
if ((flags & NetworkReachabilityFlags.IsWWAN) != 0)
return NetworkStatus.ReachableViaCarrierDataNetwork;
return NetworkStatus.ReachableViaWiFiNetwork;
}
public static NetworkStatus InternetConnectionStatus ()
{
NetworkReachabilityFlags flags;
bool defaultNetworkAvailable = IsNetworkAvailable (out flags);
if (defaultNetworkAvailable) {
if (flags.HasFlag (NetworkReachabilityFlags.IsWWAN)) {
return NetworkStatus.ReachableViaCarrierDataNetwork;
} else {
return NetworkStatus.ReachableViaWiFiNetwork;
}
} else {
return NetworkStatus.NotReachable;
}
}
public static NetworkStatus LocalWifiConnectionStatus ()
{
NetworkReachabilityFlags flags;
if (IsAdHocWiFiNetworkAvailable (out flags)){
if ((flags & NetworkReachabilityFlags.IsDirect) != 0)
return NetworkStatus.ReachableViaWiFiNetwork;
}
return NetworkStatus.NotReachable;
}
}
@madiron
Copy link

madiron commented Aug 31, 2017

SetCallback does not exists

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment