Check Network Connection with SCNetworkCheckReachabilityByName


/ Published in: Objective C
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.