Ios network Judgment Method

Source: Internet
Author: User

Ios network Judgment Method
I have summarized several methods for judging the network connection mode of ios: 1. Reachability. Reachability is a third-party library recommended by Apple to determine the current network status. The primary functions used by Reachability include:

+ (Reachability*) reachabilityWithHostName: (NSString*) hostName;+ (Reachability*) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress;+ (Reachability*) reachabilityForInternetConnection;+ (Reachability*) reachabilityForLocalWiFi;

 

Use the above function to initialize a Reachability instance, and then call 1-(NetworkStatus) currentReachabilityStatus; return NetworkStatus of Enumeration type to obtain the network connection mode, where
typedef enum {    NotReachable = 0,    ReachableViaWiFi,    ReachableViaWWAN,    ReachableVia2G,    ReachableVia3G,    ReachableVia4G,} NetworkStatus;

 

2. connectedToNetWork (from network) the second method comes from the Internet. It can only determine whether the network is connected, but cannot determine the specific connection mode. It is added to the Reachability class as a member function of the Reachablity class, the Code is as follows:
+ (BOOL)connectedToNetWork {    struct sockaddr_in zeroAddress;    bzero(&zeroAddress, sizeof(zeroAddress));    zeroAddress.sin_len = sizeof(zeroAddress);    zeroAddress.sin_family = AF_INET;    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);    SCNetworkReachabilityFlags flags;    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);    CFRelease(defaultRouteReachability);    if (!didRetrieveFlags) {        printf("Error. Count not recover network reachability flags\n");        return NO;    }    BOOL isReachable = flags & kSCNetworkFlagsReachable;    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;    return (isReachable && !needsConnection) ? YES : NO;}

 

3. statusBar + connectedToNetWork (Private API, which may be rejected by the AppStore and used with caution) the second method is convenient to use because it is defined as a class function, but has a delay, that is to say, if the user turns off the wifi switch and the traffic switch and immediately determines the network, this function will still return YES, with a latency of about 2 ~ 3 s. Therefore, the third method is to improve the accuracy of network connectivity judgment by combining statusBar and connectedToNetWork. As a Reachability class function, the Code is as follows:
+ (BOOL) isNetworkAvailable {// step1: Call the class method connectedToNetWork to determine network connectivity BOOL connected_1 = [Reachability connectedToNetWork]; // Step 2: use the Network icon on statusBar to determine network connectivity. BOOL connected_2 = YES; UIApplication * app = [UIApplication sharedApplication]; if (app. statusBarHidden = NO) {NSNumber * networkType = nil; NSArray * subViewOfStatusBar = [[app valueForKeyPath: @ "statusBar"] valueForKeyPath: @ "foregroundView"] subviews]; for (id child in subViewOfStatusBar) {if ([child isKindOfClass: NSClassFromString (@ "UIStatusBarDataNetworkItemView")]) {// 1-2G; 2-3G; 5-wifi networkType = [child valueForKeyPath: @ "dataNetworkType"]; break; }}if (networkType = nil) {connected_2 = NO ;}return (connected_1 & connected_2 );}

 

Note: If the network is disconnected, that is, the statusBar does not have a Network icon (wifi, 2G, 3G, or 4G), the subview of the foregroundView of the statusBar does not contain the UIStatusBarDataNetworkItemView, the value of networkType is the nil value assigned to the beginning, instead of 0 as the internet says.

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.