Whether the iOS test network is connected

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/lwq421336220/article/details/16982857

-(BOOL) connectedtonetwork

{

Create 0 address, 0.0.0.0 address indicates the network connection status of the query native

Structsockaddr_in zeroaddress;

Bzero (&zeroaddress,sizeof (zeroaddress));

Zeroaddress.sin_len =sizeof (zeroaddress);

Zeroaddress.sin_family =af_inet;

Scnetworkreachabilityref defaultroutereachability =scnetworkreachabilitycreatewithaddress (NULL, (STRUCTSOCKADDR *) &zeroaddress);

Scnetworkreachabilityflags flags;

BOOL didretrieveflags =scnetworkreachabilitygetflags (defaultroutereachability, &flags);

Cfrelease (defaultroutereachability);

if (!didretrieveflags)

{

printf ("Error. Could not recover network reachability FLAGSN ");

Returnno;

}

BOOL isreachable = ((Flags &kscnetworkflagsreachable)! = 0);

BOOL needsconnection = ((Flags &kscnetworkflagsconnectionrequired)! = 0);

Return (isreachable &&!needsconnection)? Yes:no;

}



When used:

if ([Selfconnectedtonetwork]) {

}else{

Uialertview *alert=[[uialertviewalloc]initwithtitle:@ "prompted" message:@ "network connection failed, please check the network connection. "Delegate:self cancelbuttontitle:@" I Know "otherbuttontitles:nil,nil";

[Alertshow];

}

Whether the iOS test network is connected

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.