2

How can I know my mac is connect to network by ethernet. Is there any method in cocoa which checks it?

I flung a wifi connection check, but no ethernet connection check.

Binarian
  • 12,012
  • 8
  • 53
  • 84
Roy
  • 49
  • 7

2 Answers2

1

Take a look at this answer, you need to add the SystemConfiguration framework to your project

Community
  • 1
  • 1
dafi
  • 3,472
  • 2
  • 26
  • 51
1

Try like this:- First import this below framework and then write the below code

#include <SystemConfiguration/SystemConfiguration.h> 

    NSString *pingHost = @"abc.apple.com"
    SCNetworkConnectionFlags flags = 0;
    if (pingHost && [pingHost length] > 0) {
        flags = 0;
        BOOL found = NO;
        SCNetworkReachabilityRef reachabilityRef = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [pingHost UTF8String]);
        if (reachabilityRef) {
            found = SCNetworkReachabilityGetFlags(reachabilityRef, &flags)
            &&  (flags & kSCNetworkFlagsReachable)
            && !(flags & kSCNetworkFlagsConnectionRequired);
            CFRelease(reachabilityRef);
            reachabilityRef = NULL;
        }
        if (found) {                
            NSLog(@"Connection established");
        }
        if (!found) {
            NSLog(@"Connection not established");
        }
    }
Perceval
  • 237
  • 2
  • 13
Hussain Shabbir
  • 14,343
  • 5
  • 36
  • 54