我怎么知道我的Mac通过以太网连接到网络。可可中有什么方法可以检查它吗?
我扔了一个wifi连接检查,但没有以太网连接检查。
最佳答案
尝试这样:-
首先导入以下框架,然后编写以下代码
#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");
}
}