• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Network Reachability
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: Network Reachability
      • From: Jens Alfke <email@hidden>
  • Prev by Date: Re: Hot to define a connection from source code?
  • Next by Date: Re: IKImageBrowserView drop operation always highlighting a specific row
  • Previous by thread: Re: [MEET] February CocoaHeads Developer Meetings
  • Next by thread: Re: Network Reachability
  • Index(es):
    • Date
    • Thread