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