Checking Network Status with CFNetDiagnostics

Here's a quick and dirty technique for checking the current status of the network connection. This example will only work in Tiger and up, and doesn't use the interactive network troubleshooting UI.

#import "Controller.h"
#import <CoreServices/CoreServices.h>

@implementation Controller

- (IBAction)click:(id)sender
{
  NSURL * url = [NSURL URLWithString:@"http://apple.com"];
  [self networkIsAvailableForURL: url];
}

- (BOOL) networkIsAvailableForURL: (NSURL *)url
{
  CFNetDiagnosticRef diag;        
  diag = CFNetDiagnosticCreateWithURL (NULL, (CFURLRef)url);
  
  CFNetDiagnosticStatus status;
  status = CFNetDiagnosticCopyNetworkStatusPassively (diag, NULL);        
  
  CFRelease (diag);
  
  if ( status == kCFNetDiagnosticConnectionUp )
  {
    NSLog (@"Connection is up");
    return YES;
  } else {
    NSLog (@"Connection is down");
    return NO;
  }
}

@end


You'll need to add the CoreServices framework to your project before this will work. You can do that like this:

1. Select the Frameworks group in the main Xcode window
2. Choosing Project > Add to Project...
3. In the file dialog, go to /System/Library/Frameworks and select CoreServices.framework

One other caveat with this approach is that it doesn't seem to differentiate between a wireless connection that is working, and one that is active but doesn't have a valid route to the internet. There might be a way to deal with that, but I'm not sure what it is off the top of my head.
Design Element
Checking Network Status with CFNetDiagnostics
Posted Dec 17, 2005 — 11 comments below




 

SeoxyS — Dec 18, 05 615

Hi Scott,

Is there a way to check to connectiction that is Panther-friendly?

Why just don't you make for example that:

NSURL *url = [NSURL urlWithString:@"http://www.seosoft.info/app_rsrc/exces_version.plist"]; //this is the url of a plist file that really exists. but you can use any plist file
NSDictionary *dict = [[[NSDictionary alloc] initWithContentsOfURL:url] autorelease];
if(dict)
NSLog(@"Connexion to seosoft.info is up");
else
NSLog(@"Connexion to seosoft.info is down

John Pannell — Dec 18, 05 616

I found this snippet on the mailing lists some time ago...

BOOL networkReachableWithoutAnythingSpecialHappening(void)
{
Boolean Success;
SCNetworkConnectionFlags ReachabilityStatus;
Success = SCNetworkCheckReachabilityByName("www.positivespinmedia.com",
&ReachabilityStatus);
return (Success && (ReachabilityStatus & 3));
}

You must include the SystemConfiguration framework in the project - same steps that Scott described. Panther and Tiger compatible.

Marc Charbonneau — Dec 18, 05 617

I've looked into the topic a little, and as far as I can tell there is no "perfect" way of checking network availability. Any way you do it, there's always going to be possible edge cases (like the wireless issue mentioned in this entry).

My thoughts are that it's best to keep things simple instead of trying to be perfect. This seems like a good way to go about doing it, but however you accomplish it, spend your time putting your network code in a separate thread, providing user feedback, and failing gracefully, rather than trying to come up with a perfect detection mechanism.

Scott Stevenson — Dec 19, 05 618 Scotty the Leopard

I'm aware of the SC framework approach, but there was some reason I preferred the CFNetDiagnostics approach. Can't remember why at the moment, though

nlm — Dec 19, 05 619

How about trying wiki.hping.org/25/ as an alternative? (There is a GUI version called XHping, homepage.mac.com/stephaneleon/.)

Nic du Plessis — Aug 20, 06 1619

Does this method actually check whether the URL can be reached or does it just check for a connection? I keep getting false positives on URL's that don't exist.

Scott Stevenson — Aug 30, 06 1665 Scotty the Leopard

Does this method actually check whether the URL can be reached

I don't believe so. The intention is just to see if the network is up.

John Joyce — Jan 24, 08 5395

This could be useful, but it would be nice if there were simply a convenience method to do all of this.

Then, if a request/response cycle, like http, does not succeed, this would be a useful call to check and see if the mac is connected to a network.

If network is connected, and request / response fails, then there is an obvious and simple error message: "You are connected to a network but the network may not be connected to the internet or <the server requested> is unavailable.

John Joyce — Jan 26, 08 5398

This could be useful, but it would be nice if there were simply a convenience method to do all of this.

Then, if a request/response cycle, like http, does not succeed, this would be a useful call to check and see if the mac is connected to a network.

If network is connected, and request / response fails, then there is an obvious and simple error message: "You are connected to a network but the network may not be connected to the internet or <the server requested> is unavailable.

Custom paper writing — Jan 06, 10 7083

Thanks for great tips!
Buy essay

celik kapi — Jan 11, 10 7092

çelik kapı modeli ya da
çelik kapı aksesuarından, çelik kapı müşterisi de herhangi
bir tüketiciden daha fazlası olduğunun bilincindeyiz.
Çelik kapı, taşınmaz sınıfına giren demirbaş sayılan ayrıcalıklı bir üründür.




 

Comments Temporarily Disabled

I had to temporarily disable comments due to spam. I'll re-enable them soon.





Copyright © Scott Stevenson 2004-2015