For iOS to determine network connectivity I've summed up several methods:
1, reachability.
Reachability is a third-party library recommended by Apple to determine the current state of the network. The main functions used by reachability include:
+ (reachability*) Reachabilitywithhostname: (nsstring*) hostname;+ (reachability*) reachabilitywithaddress: (const struct sockaddr_in*) hostaddress;+ (reachability*) reachabilityforinternetconnection;+ (reachability*) Reachabilityforlocalwifi;
Initializes a reachability instance with the above function, and then calls the
-(NetworkStatus) currentreachabilitystatus;
Returns the enumeration type NetworkStatus for network connectivity, where
typedef enum {notreachable = 0, Reachableviawifi, Reachableviawwan, reachablevia2g, REACHABLEVIA3G, Rea CHABLEVIA4G,} networkstatus;
2. Connectedtonetwork (from Network)
The second way comes from the Internet, and can only judge whether the network is connected and can not determine the specific connectivity, as a member function of the Reachablity class is added to the Reachability 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, may be rejected by AppStore, use caution)
The second way is defined as a class function, so it is convenient to use, but there will be delay, that is, if the user switched off the WiFi switch and the flow switch and then immediately network judgment, then this function will still return yes, the delay is about 2~3s. So the third way is to improve the accuracy of network connectivity judgment by combining StatusBar and connectedtonetwork. is still a class function as a reachability, the code is as follows:
+ (BOOL) ISNETWORKAVAILABLE{    //STEP1: Call class method connectedtonetwork determine network connectivity BOOL connected_1=[Reachability connectedToNetWork];   //STEP2: Use the network icon on the 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);}
description : In the case of a broken network, that is, there is no network icon on the StatusBar (WiFi, 2G, 3G or 4G), Then StatusBar Foregroundview Subview is not uistatusbardatanetworkitemview, so that the value of Networktype will be the first assignment of nil, And not like on the internet to say 0.
This article is from the "Layne Learning Corner" blog, please be sure to keep this source http://laynestone.blog.51cto.com/9459455/1695995
How iOS networks are judged