Posted By

warhero on 12/26/08


Tagged

network objc connected


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

Blaenk
tbass134
alladinian
trawor
cnathan


Check Network Connection with SCNetworkCheckReachabilityByName


 / Published in: Objective C
 

  1. #import "AppController.h"
  2. #import <CoreFoundation/CoreFoundation.h>
  3. #import <SystemConfiguration/SCNetworkReachability.h>
  4.  
  5. @implementation AppController
  6.  
  7. - (IBAction)checkConnection:(id)sender
  8. {
  9. NSAlert *alert = [[[NSAlert alloc] init] autorelease];
  10. [alert setAlertStyle:NSInformationalAlertStyle];
  11. [alert addButtonWithTitle:@"Ok"];
  12. [alert setInformativeText:@"Connection Test"];
  13.  
  14. const char *hostname = "google.com";
  15. SCNetworkConnectionFlags flags;
  16. if(SCNetworkCheckReachabilityByName(hostname, &flags))
  17. {
  18. if(kSCNetworkFlagsReachable & flags && !(kSCNetworkFlagsConnectionRequired & flags))
  19. {
  20. [alert setMessageText:@"You are connected!"];
  21. [alert runModal];
  22. return;
  23. }
  24. }
  25. [alert setMessageText:@"You are not connected!"];
  26. [alert runModal];
  27. }
  28. @end

Report this snippet  

You need to login to post a comment.