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.
Checking Network Status with CFNetDiagnostics
Posted Dec 17, 2005 — 11 comments below
Posted Dec 17, 2005 — 11 comments below
SeoxyS — Dec 18, 05 615
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
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
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
nlm — Dec 19, 05 619
Nic du Plessis — Aug 20, 06 1619
Scott Stevenson — Aug 30, 06 1665
I don't believe so. The intention is just to see if the network is up.
John Joyce — Jan 24, 08 5395
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
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
Buy essay
celik kapi — Jan 11, 10 7092
ç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.