Network Reachability
Network Reachability
- Subject: Network Reachability
- From: Laurent Daudelin <email@hidden>
- Date: Thu, 28 Jan 2010 21:22:37 -0800
Hello.
Earlier this week, I asked how I could determine network reachability from my app. Someone replied to check the SystemConfiguration framework. I finally got around to have a look and wrote the following code:
+ (BOOL)hostIsReachable:(NSString *)hostName
{
CFHostRef host;
assert(hostName != NULL);
/* Creates a new host object with the given name. */
host = CFHostCreateWithName(kCFAllocatorDefault, (CFStringRef)hostName);
assert(host != NULL);
SCNetworkReachabilityRef target = SCNetworkReachabilityCreateWithName(NULL, [hostName cStringUsingEncoding:NSUTF8StringEncoding]);
assert( target != NULL);
SCNetworkConnectionFlags flags;
SCNetworkReachabilityGetFlags(target, &flags);
BOOL result = (flags & kSCNetworkFlagsReachable);
NSLog(@"PLHostReachability: host %@ is reachable: %@", hostName, result ? @"yes" : @"no");
CFRelease(target);
return result;
}
However, no matter if I'm connected to the network or not, SCNetworkReachabilityGetFlags() always returns any host to be reachable. Am I missing something? Should I look for other flags? Any way to do this in Cocoa?
Thanks in advance!
-Laurent.
--
Laurent Daudelin
AIM/iChat/Skype:LaurentDaudelin http://nemesys.dyndns.org
Logiciels Nemesys Software email@hidden
Photo Gallery Store: http://laurentdaudelin.shutterbugstorefront.com/g/galleries
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden