IOS determines whether a network exists

Source: Internet
Author: User

IOS determines whether a network exists

Every iPhone developer that has integrated a network connection based application has had to follow the Apple HID (Human Interface Design) rules. this means, that in order to get the Apple reviewers to sign-off on your application, you have to pass the network connectivity test. basically, in the case of a catastrophic network disconnect issue with 3G and WiFi does your application properly alert t He user that it will not perform properly without the usage of the network? I 've seen a lot of workaround techniques where users attempt to do an HTTP GET on say Google or some other website. clearly, this kind of technique will work sporadically in an 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 will probably believe that the application is hung. the ideal solution happens to be 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
  2. # Import
  3. # Import
  4. # Import
  5. # Import
  6. // Snip, you know we're in the implementation...

    # Pragma mark-whether a network exists

    -(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)

    {

    ReturnNO;

    }


    BOOL isReachable = flags & kSCNetworkFlagsReachable;

    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;

    Return (isReachable &&! NeedsConnection )? YES: NO;

    }



    It is a good way to improve yourself and help others.


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.