iOS Guides | Samples

SystemConfiguration.NetworkReachability Class

Used to detect the reachability of the network and to get notifications on network reachability changes.

See Also: NetworkReachability

Syntax

public class NetworkReachability : INativeObject, IDisposable

Remarks

You instantiate this class with a hostname or an IP address, and then use the NetworkReachability.TryGetFlags to get the reachability status. 

To be notified of changes in the reachability of the specified host, you use the NetworkReachability.SetNotification method to register a callback that will be invoked when there is a network reachability event and then call one of the NetworkReachability.Schedule methods to start the event delivery.   Additionally,  NetworkReachability.SetDispatchQueue can be used to configure the queue upon which the notification is invoked.

You can detect the ad-hoc WiFi network using the IP address 169.254.0.0 and the general network availability with 0.0.0.0. 

C# Example

// Track the reachability to a specific host
void Setup ()
{
    reachability = new NetworkReachability ("www.google.com");
    
    // Need to probe before we queue, or we wont get any meaningful values
    // this only happens when you create NetworkReachability from a hostname
    reachable = reachability.TryGetFlags (out flags);
    
    reachability.SetNotification (OnChange);
    reachability.Schedule (CFRunLoop.Current, CFRunLoop.ModeDefault);
}

void OnChange (NetworkReachabilityFlags flags)
{
    Console.WriteLine ("New state for host: " + flags);
}

Related content

Requirements

Namespace: SystemConfiguration
Assembly: monotouch (in monotouch.dll)
Assembly Versions: 0.0.0.0

The members of SystemConfiguration.NetworkReachability are listed below.

See Also: Object

Public Constructors

Creates a network reachability class based on an IP address.
Creates a network reachability object from a hostname.
Creates a network reachability object from a local IP address and a remote one.

Public Properties

[read-only]
HandleIntPtr. Handle (pointer) to the unmanaged object representation.

Public Methods

Dispose()
Releases the resources used by the NetworkReachability object.
Dispose(Boolean)
Releases the resources used by the NetworkReachability object.
Finalize()
Finalizer for the NetworkReachability object
GetFlags(out NetworkReachabilityFlags) : StatusCode
Method used to get the current reachability flags for this host.
Schedule() : Boolean
Schedules the delivery of the events (what is set with SetCallback) on the current loop.
Schedule(CFRunLoop, String) : Boolean
Schedules the delivery of the events (what is set with SetCallback) on the given run loop.
SetCallback(NetworkReachability+Notification) : Boolean
Configures a callback to be invoked when the reachability changes.
SetDispatchQueue(DispatchQueue) : Boolean
Specifies the DispatchQueue to be used for callbacks.
SetNotification(NetworkReachability+Notification) : StatusCode
Configures the method to be invoked when network reachability changes.
TryGetFlags(out NetworkReachabilityFlags) : Boolean
Method used to get the current reachability flags for this host.
Unschedule() : Boolean
Removes the NetworkRechability from the given run loop.
Unschedule(CFRunLoop, String) : Boolean
Removes the NetworkRechability from the given run loop.