In the actual development, there will be a requirement: the user in a network state will be directly from the network request data, in the case of no network directly from the local reading data.
The method below can be used to determine if there is a network.
-(BOOL) connectedtonetwork{//Create Zero Addy structsockaddr_in zeroaddress; Bzero (&zeroaddress,sizeof(zeroaddress)); Zeroaddress.sin_len=sizeof(zeroaddress); Zeroaddress.sin_family=af_inet; //Recover reachability FlagsScnetworkreachabilityref defaultroutereachability = scnetworkreachabilitycreatewithaddress (NULL, (structSOCKADDR *) &zeroaddress); Scnetworkreachabilityflags flags; BOOL Didretrieveflags= Scnetworkreachabilitygetflags (defaultroutereachability, &flags); Cfrelease (defaultroutereachability); if(!didretrieveflags) {printf ("Error. Could not recover network reachability flags\n"); returnNO; } BOOL isreachable= ((Flags & kscnetworkflagsreachable)! =0); BOOL needsconnection= ((Flags & kscnetworkflagsconnectionrequired)! =0); return(isreachable &&!needsconnection)?Yes:no;}
You can copy the file in the file, The method has a return value of type bool, which only needs to be performed according to the return value.
How iOS learns to determine if there is a network