iOS to determine if there is a network

Source: Internet
Author: User

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.

    1. #import <sys/socket.h>
    2. #import <netinet/in.h>
    3. #import <arpa/inet.h>
    4. #import <netdb.h>
    5. #import <SystemConfiguration/SCNetworkReachability.h>
    6. 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

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.