1 //determine if the network is connected2+(BOOL) connectedtonetwork3 {4 //Create 0 address, 0.0.0.0 address indicates the network connection status of the query native5 structSockaddr_in zeroaddress;//structs are used to pass complex parameters to a method (using parameters as objects, so that they are easy to extend)6Bzero (&zeroaddress,sizeof(zeroaddress));7Zeroaddress.sin_len =sizeof(zeroaddress);8zeroaddress.sin_family =af_inet;9 Ten //Recover reachability Flags OneScnetworkreachabilityref defaultroutereachability = scnetworkreachabilitycreatewithaddress (NULL, (structSOCKADDR *) &zeroaddress); A scnetworkreachabilityflags flags; - //get a connection flag -BOOL didretrieveflags = Scnetworkreachabilitygetflags (defaultroutereachability, &flags); the cfrelease (defaultroutereachability); - //If you cannot get the connection flag, you cannot connect to the network and return directly - if(!didretrieveflags) - { + returnNO; - } + //Judging by the connection marks obtained ABOOL isreachable = Flags &kscnetworkflagsreachable; atBOOL needsconnection = Flags &kscnetworkflagsconnectionrequired; -BOOL Iswwan = Flags &Kscnetworkreachabilityflagsiswwan; - return(isreachable && (!needsconnection | | iswwan))?Yes:no; -}
IOS determines if the network is connected