Iphone development uses Reachability to determine the network status

Source: Internet
Author: User

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];

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.