detecting no internet connection

  • Hi,

    Does anyone know a good way to detect whether a user has an internet
    connection?  NSURL does the right thing unless there is an active
    airport with no connection.  I tried using NSURLRequest.  That is a
    little better, but it doesn't seem to make any difference what number
    I use for the timeout value.  It seems to try for an awfully long
    time before it gives up.  Any suggestions are greatly appreciated.

    Thanks,
    Denise
  • On 8-Dec-07, at 12:14 PM, Denise Eatherly wrote:

    > Hi,
    >
    > Does anyone know a good way to detect whether a user has an internet
    > connection?  NSURL does the right thing unless there is an active
    > airport with no connection.  I tried using NSURLRequest.  That is a
    > little better, but it doesn't seem to make any difference what
    > number I use for the timeout value.  It seems to try for an awfully
    > long time before it gives up.  Any suggestions are greatly
    > appreciated.

    I'm currently using something like this:

    - (BOOL) isNetworkAvailable
    {
        BOOL    result = NO;
        SCNetworkConnectionFlags    flags;
        NSString *network;

        network = someNetwork; // assume exists

        if ( SCNetworkCheckReachabilityByName([network cString], &flags) )
        {
            result =    !(flags & kSCNetworkFlagsConnectionRequired) &&
    (flags & kSCNetworkFlagsReachable);
        }
        return result;
    }

    ;david

    --
    David LeBer
    Codeferous Software
    'co-def-er-ous' adj. Literally 'code-bearing'
    site:  http://codeferous.com
    blog: http://davidleber.net
    profile: http://www.linkedin.com/in/davidleber
    --
    Toronto Area Cocoa / WebObjects developers group:
    http://tacow.org
  • On Dec 8, 2007, at 9:32 AM, David LeBer wrote:

    >
    > On 8-Dec-07, at 12:14 PM, Denise Eatherly wrote:
    >
    >> Hi,
    >>
    >> Does anyone know a good way to detect whether a user has an
    >> internet connection?  NSURL does the right thing unless there is an
    >> active airport with no connection.  I tried using NSURLRequest.
    >> That is a little better, but it doesn't seem to make any difference
    >> what number I use for the timeout value.  It seems to try for an
    >> awfully long time before it gives up.  Any suggestions are greatly
    >> appreciated.
    >
    >
    >
    > I'm currently using something like this:
    >
    > - (BOOL) isNetworkAvailable
    > {
    > BOOL    result = NO;
    > SCNetworkConnectionFlags    flags;
    > NSString *network;
    >
    > network = someNetwork; // assume exists
    >
    > if ( SCNetworkCheckReachabilityByName([network cString], &flags) )
    > {
    > result =    !(flags & kSCNetworkFlagsConnectionRequired) &&
    > (flags & kSCNetworkFlagsReachable);
    > }
    > return result;
    > }
    >
    > ;david
    >
    > --
    > David LeBer

    Hi,

    Note that the use of -[NSString cString] is rather deprecated at the
    moment (as of OS X 10.4), in favor of - (const char
    *)cStringUsingEncoding:(NSStringEncoding)encoding.

    Cheers,
    Andrew
  • On Dec 8, 2007, at 9:14 AM, Denise Eatherly wrote:

    > Does anyone know a good way to detect whether a user has an internet
    > connection?  NSURL does the right thing unless there is an active
    > airport with no connection.  I tried using NSURLRequest.  That is a
    > little better, but it doesn't seem to make any difference what
    > number I use for the timeout value.  It seems to try for an awfully
    > long time before it gives up.  Any suggestions are greatly
    > appreciated.

    There really isn't any good way to do this, based on the architecture
    of the Internet itself.  Your best bet is to check the network
    reachability (using the SystemConfiguration.framework functions
    mentioned by David LeBer) for the specific host you're trying to access.

    To expand a bit:  You can be on the Internet, but with something along
    your route that prevents a specific host from being reachable.  You
    can be on a private network -- even one with self-assigned or
    otherwise-unroutable IP addresses, such as those in the 10.* range or
    192.168.* range -- that prevents general Internet access, but creates
    tunnels on demand to specific hosts.  And so on.

    Fundamentally, you can't anticipate all such situations at design
    time.  So rather than trying to detect whether there is an Internet
    connection (for some specific definition of "Internet connection" that
    may not match the user's network), you need to code to be as robust as
    possible:  Perform specific actions against specific hosts, generally
    in a concurrent fashion, and handle the exceptional cases appropriately.

      -- Chris
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today