As we all know, in the development of the app, when involved in the network connection, will be thinking ahead of time to determine the current network connection status, if there is no network, no longer request the URL, eliminating unnecessary steps, so, this how to judge? It's actually very simple.
Premise: Project add: Systemconfiguration.framework
Then include the header file in the class that needs to be judged:
#import "Reachability.h"
Here's a way I wrote:
#pragma mark-Detect network Connections
+ (BOOL) isconnectionavailable
{
BOOL isexistencenetwork = YES;
Ddreachability *reach = [ddreachability reachabilitywithhostname:@ "www.apple.com"];
switch ([reach Currentreachabilitystatus]) {
Case Notreachable:
Isexistencenetwork = NO;
NSLog (@ "notreachable");
Break
Case Reachableviawifi:
Isexistencenetwork = YES;
NSLog (@ "WIFI");
Break
Case Reachableviawwan:
Isexistencenetwork = YES;
NSLog (@ "3G");
Break
}
if (!isexistencenetwork) {
Uialertview *alertview = [[Uialertview alloc] Initwithtitle:nil message:@ "The current network is not available, please check the network connection!" Delegate:nil cancelbuttontitle:@ "determine" otherbuttontitles:nil];
[Alertview show];
return NO;
}else{
return isexistencenetwork;
}
}
Then in the place where you need to judge directly:
Can be networked to request data
if ([self isconnectionavailable]) {
}
You see, it's so simple.
So extrapolate, if you are not only to judge whether the network unobstructed, but to judge is WiFi or 3G, and then write a Isenablewifi method, the specific method of judgment will not have to repeat it, currentreachabilitystatus judgment.
How to determine the current network connection status (network is OK)