Every IPhone developer that have integrated a network connection based application have had to follow the Apple HID (Human I Nterface Design) rules. This means, which in order to get the Apple reviewers to Sign-off on your application, you have to pass the network Connect Ivity test. Basically, in the case of a catastrophic network disconnect issue with 3G and WiFi does your application properly alert th E user that it would not perform properly without the the the network? I ' ve seen a lot of workaround techniques where the users attempt to does an HTTP GET on say Google or some other website. Clearly, this kind of technique would work sporadically in a unknown environment common to the mobile device. I mean, imagine causing your application users to sit through a 5-30 second Web request timeout. The end-user would probably believe that's the application is hung. The ideal solution happens to being handed to us in the API Framework scnetworkreachability. (Make sure your codebase is linked properly with the "systemconfiguration" Framework).
It ' s worked for me and I recommend it for doing a simple network connectivity test.
- #import <sys/socket.h>
- #import <netinet/in.h>
- #import <arpa/inet.h>
- #import <netdb.h>
- #import <SystemConfiguration/SCNetworkReachability.h>
- Snip, you know we ' re in the implementation ...
#pragma mark- is there a network
-(BOOL) isnetworkreachable{
//Create Zero Addy
struct sockaddr_in zeroaddress;
bzero (&zeroaddress,sizeof(zeroaddress));
Zeroaddress. Sin_len =sizeof(zeroaddress);
Zeroaddress. sin_family =af_inet;
Recover reachability Flags
scnetworkreachabilityref defaultroutereachability =scnetworkreachabilitycreatewithaddress( NULL, (structsockaddr *) &zeroaddress);
scnetworkreachabilityflags flags;
BOOL didretrieveflags =scnetworkreachabilitygetflags(defaultroutereachability, &flags);
Cfrelease (defaultroutereachability);
if (!didretrieveflags)
{
return NO;
}
BOOL isreachable = flags &kscnetworkflagsreachable;
BOOL needsconnection = flags &kscnetworkflagsconnectionrequired;
return (isreachable &&!needsconnection)? YES : NO;
}
See this method very good to up to improve themselves, convenient for others, is for remembering.
iOS to determine if there is a network