Copy Reachability. h and Reachability. m to the project.
2. Add the framework:
Add SystemConfiguration. framework to the project.
The code in the example is described here.
The following code sets the UITextField value and the displayed image.
[Cpp]
-(Void) configureTextField: (UITextField *) textField imageView: (UIImageView *) imageView reachability: (Reachability *) curReach
{
NetworkStatus netStatus = [curReach currentReachabilityStatus];
BOOL connectionRequired = [curReach connectionRequired];
NSString * statusString = @"";
Switch (netStatus)
{
Case NotReachable:
{
StatusString = @ "Access Not Available ";
ImageView. image = [UIImage imageNamed: @ "stop-32.png"];
// Minor interface detail-connectionRequired may return yes, even when the host is unreachable. We cover that up here...
ConnectionRequired = NO;
Break;
}
Case ReachableViaWWAN:
{
StatusString = @ "Reachable WWAN ";
ImageView. image = [UIImage imageNamed: @ "WWAN5.png"];
Break;
}
Case ReachableViaWiFi:
{
StatusString = @ "Reachable WiFi ";
ImageView. image = [UIImage imageNamed: @ "Airport.png"];
Break;
}
}
If (connectionRequired)
{
StatusString = [NSString stringWithFormat: @ "% @, Connection Required", statusString];
}
TextField. text = statusString;
}
-(Void) configureTextField: (UITextField *) textField imageView: (UIImageView *) imageView reachability: (Reachability *) curReach
{
NetworkStatus netStatus = [curReach currentReachabilityStatus];
BOOL connectionRequired = [curReach connectionRequired];
NSString * statusString = @"";
Switch (netStatus)
{
Case NotReachable:
{
StatusString = @ "Access Not Available ";
ImageView. image = [UIImage imageNamed: @ "stop-32.png"];
// Minor interface detail-connectionRequired may return yes, even when the host is unreachable. We cover that up here...
ConnectionRequired = NO;
Break;
}
Case ReachableViaWWAN:
{
StatusString = @ "Reachable WWAN ";
ImageView. image = [UIImage imageNamed: @ "WWAN5.png"];
Break;
}
Case ReachableViaWiFi:
{
StatusString = @ "Reachable WiFi ";
ImageView. image = [UIImage imageNamed: @ "Airport.png"];
Break;
}
}
If (connectionRequired)
{
StatusString = [NSString stringWithFormat: @ "% @, Connection Required", statusString];
}
TextField. text = statusString;
}
Here we can display the UITextField and the image.
[Cpp]
-(Void) updateInterfaceWithReachability: (Reachability *) curReach
{
If (curReach = hostReach)
{
[Self configureTextField: remoteHostStatusField imageView: remoteHostIcon reachability: curReach];
NetworkStatus netStatus = [curReach currentReachabilityStatus];
BOOL connectionRequired = [curReach connectionRequired];
SummaryLabel. hidden = (netStatus! = ReachableViaWWAN );
NSString * baseLabel = @"";
If (connectionRequired)
{
BaseLabel = @ "Cellular data network is available. \ n Internet traffic will be routed through it after a connection is established .";
}
Else
{
BaseLabel = @ "Cellular data network is active. \ n Internet traffic will be routed through it .";
}
SummaryLabel. text = baseLabel;
}
If (curReach = internetReach)
{
[Self configureTextField: internetConnectionStatusField imageView: internetConnectionIcon reachability: curReach];
}
If (curReach = wifiReach)
{
[Self configureTextField: localWiFiConnectionStatusField imageView: localWiFiConnectionIcon reachability: curReach];
}
}
-(Void) updateInterfaceWithReachability: (Reachability *) curReach
{
If (curReach = hostReach)
{
[Self configureTextField: remoteHostStatusField imageView: remoteHostIcon reachability: curReach];
NetworkStatus netStatus = [curReach currentReachabilityStatus];
BOOL connectionRequired = [curReach connectionRequired];
SummaryLabel. hidden = (netStatus! = ReachableViaWWAN );
NSString * baseLabel = @"";
If (connectionRequired)
{
BaseLabel = @ "Cellular data network is available. \ n Internet traffic will be routed through it after a connection is established .";
}
Else
{
BaseLabel = @ "Cellular data network is active. \ n Internet traffic will be routed through it .";
}
SummaryLabel. text = baseLabel;
}
If (curReach = internetReach)
{
[Self configureTextField: internetConnectionStatusField imageView: internetConnectionIcon reachability: curReach];
}
If (curReach = wifiReach)
{
[Self configureTextField: localWiFiConnectionStatusField imageView: localWiFiConnectionIcon reachability: curReach];
}
} When the network status changes, the status and UITextField and image display are changed.
[Cpp]
-(Void) reachabilityChanged: (NSNotification *) note
{
Reachability * curReach = [note object];
NSParameterAssert ([curReach isKindOfClass: [Reachability class]);
[Self updateInterfaceWithReachability: curReach];
}
-(Void) reachabilityChanged: (NSNotification *) note
{
Reachability * curReach = [note object];
NSParameterAssert ([curReach isKindOfClass: [Reachability class]);
[Self updateInterfaceWithReachability: curReach];
}
If we want to detect the current network status in real time, we need to notify the current network status through the registration notification. NSNotification is used here.
In the following code, call reachabilityChanged after the notification is registered: This method detects the network and updates the network display status.
[Cpp]
-(Void) applicationDidFinishLaunching: (UIApplication *) application
{
# Pragma unused (application)
ContentView. backgroundColor = [UIColor groupTableViewBackgroundColor];
SummaryLabel. hidden = YES;
// Observe the kNetworkReachabilityChangedNotification. When that notification is posted,
// Method "reachabilityChanged" will be called.
[[Nsicationicationcenter defacenter center] addObserver: self selector: @ selector (reachabilityChanged :) name: kReachabilityChangedNotification object: nil];
// Change the host name here to change the server your monitoring
RemoteHostLabel. text = [NSString stringWithFormat: @ "Remote Host: % @", @ "www.hopean.com"];
HostReach = [[Reachability reachabilityWithHostName: @ "www.hopesn.com"] retain];
[HostReach startNotifier];
[Self updateInterfaceWithReachability: hostReach];
InternetReach = [[Reachability reachabilityForInternetConnection] retain];
[InternetReach startNotifier];
[Self updateInterfaceWithReachability: internetReach];
WifiReach = [[Reachability reachabilityForLocalWiFi] retain];
[WifiReach startNotifier];
[Self updateInterfaceWithReachability: wifiReach];
[Window makeKeyAndVisible];
}
-(Void) applicationDidFinishLaunching: (UIApplication *) application
{
# Pragma unused (application)
ContentView. backgroundColor = [UIColor groupTableViewBackgroundColor];
SummaryLabel. hidden = YES;
// Observe the kNetworkReachabilityChangedNotification. When that notification is posted,
// Method "reachabilityChanged" will be called.
[[Nsicationicationcenter defacenter center] addObserver: self selector: @ selector (reachabilityChanged :) name: kReachabilityChangedNotification object: nil];
// Change the host name here to change the server your monitoring
RemoteHostLabel. text = [NSString stringWithFormat: @ "Remote Host: % @", @ "www.hopean.com"];
HostReach = [[Reachability reachabilityWithHostName: @ "www.hopesn.com"] retain];
[HostReach startNotifier];
[Self updateInterfaceWithReachability: hostReach];
InternetReach = [[Reachability reachabilityForInternetConnection] retain];
[InternetReach startNotifier];
[Self updateInterfaceWithReachability: internetReach];
WifiReach = [[Reachability reachabilityForLocalWiFi] retain];
[WifiReach startNotifier];
[Self updateInterfaceWithReachability: wifiReach];
[Window makeKeyAndVisible];
}