As we all know, when developing an APP that involves network connections, we all want to determine the current network connection status in advance. If there is no network, we will no longer request a url, saving unnecessary steps, so how can we determine this? It is actually very simple.
Prerequisite: Add SystemConfiguration. framework to the Project
Search for Reachability in the Apple API, download the demo, and drag the Reachability. h and Reachability. m in the demo to your project.
Then include the header file in the class to be judged:
#import "Reachability.h"
[If you use the ASIHTTPRequest class library, you can directly import Reachbility. h. The ASIHTTP class library contains Reachbility. h and. m]
-(BOOL) isConnectionAvailable {BOOL isExistenceNetwork = YES; Reachability * reach = [Reachability failed: @ "www.apple.com"]; switch ([reach currentReachabilityStatus]) {case NotReachable: Failed = NO; // NSLog (@ "notReachable"); break; case ReachableViaWiFi: isExistenceNetwork = YES; // NSLog (@ "WIFI"); break; case ReachableViaWWAN: isExistenceNetwork = YES; // NSLog (@ "3G "); Break;} if (! IsExistenceNetwork) {MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo: self. view animated: YES]; // MBProgressHUD is a third-party library. You do not need to omit or use AlertView hud. removeFromSuperViewOnHide = YES; hud. mode = MBProgressHUDModeText; hud. labelText = @ "the current network is unavailable. Check the network connection"; // The content hud. minSize = CGSizeMake (132.f, 108.0f); [hud hide: YES afterDelay: 3]; return NO;} return isExistenceNetwork ;}
Then, go directly to [self isConnectionAvailable] Where you need to judge it. You can understand it. that's simple.
Therefore, if you determine whether the network is smooth, but whether it is WIFI or 3 GB, write another isEnableWIFI method. You don't need to repeat the specific method, currentReachabilityStatus.