IOS determines if the network is connected

Source: Internet
Author: User

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

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.