First look at Reachability. h found
# Import <Foundation/Foundation. h>
# Import <SystemConfiguration/SystemConfiguration. h>
# Import <netinet/in. h>
So if we need to use this class in the project, we need to introduce SystemConfiguration. framework.
In ios network development, you can determine the network environment and connection conditions (no network connection, 3G, WIFI, GPRS)
Enum {// DDG NetworkStatus Constant Names.
KNotReachable = 0,
// Apple's code depends upon 'notreachable' being the same value as 'no '.
KReachableViaWWAN, // Switched order from Apple's enum. WWAN is active before WiFi.
KReachableViaWiFi
};
Define three network types:
I. kNotReachable no network connection
Ii. kReachableViaWWAN uses GPRS or 3G network connection
Iii. kReachableViaWiFi connection
I used the following statement in the project to determine whether a network connection exists.
BOOL reachable = [[Reachability
ReachabilityForInternetConnection] isReachable];
If (! Reachable ){
UIAlertView * alertView = [[UIAlertView
Alloc] initWithTitle: @ "this function can be used only when it is connected to the network. Check your network connection status." message: nil delegate: nil cancelButtonTitle: @ "OK"
OtherButtonTitles: nil]
Autorelease];
[AlertView show];
Return;
}
When there is a network request, the method in the class can return the current network connection status
Example: Reachable * reachable = [Reachable reachabilityWithHostName: @ "http://blog.csdn.net"];
Then we apply
// These are the status tests.
-(Networkstatus) currentreachabilitystatus; returns the network connection status.
Switch ([reachable currentreachabilitystatus]) {
/* [Reachable currentreachabilitystatus] contains three values
I. knotreachable no network connection
Ii. kreachableviawwan uses GPRS or 3G network connection
Iii. kreachableviawifi connection */
}