How to determine the current network status in IOS

Source: Internet
Author: User

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.


Related Article

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.