0

In my App, I wanted to check if there any type of existing network available in current OS X system. Ethernet or Internet or Wifi or 3G-card or any other type of network communication.

How can I achieve that?

Thank you!


https://stackoverflow.com/questions/5662298...

Thank bdash and cody! I do my homework and found another answer above. But that is for iPhone, I make some modification below.

- (BOOL)isAnyNetworkExist
{
    struct sockaddr_in nullAddress;

    bzero(&nullAddress, sizeof(nullAddress));
    nullAddress.sin_len = sizeof(nullAddress);
    nullAddress.sin_family = AF_INET;

    SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*) &nullAddress);

    SCNetworkReachabilityFlags flags;
    SCNetworkReachabilityGetFlags(ref, &flags);
    CFRelease(ref);     /* !!! */

    AMCDebug(@"Flag: 0x%08X", flags);
    if (0 != (flags & kSCNetworkFlagsIsLocalAddress))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

I check the "kSCNetworkFlagsIsLocalAddress" ONLY, but I am not sure if this OK.

I tried the flag when I remove all network connection and it returned 0x07.

Community
  • 1
  • 1
Andrew Chang
  • 1,289
  • 1
  • 18
  • 38
  • This is either a duplicate of either http://stackoverflow.com/questions/7627058/how-to-determine-internet-connection-in-cocoa or http://stackoverflow.com/questions/4367927/get-a-list-of-all-available-network-interfaces-en0-en1-en2-etc-with-cocoa/. It's not clear from the question which is most appropriate. – bdash Apr 10 '13 at 07:52
  • No, the link uses shell command to do so. What I wanted is the way in app runtime way. Perhaps this may work [http://stackoverflow.com/questions/4021095/...](http://stackoverflow.com/questions/4021095/using-cocoa-objective-c-get-currently-connected-networks-security-type-in-ma/4024962#4024962). I will keep searching... – Andrew Chang Apr 11 '13 at 00:20
  • None of the linked questions require shell commands. Every single one of them has answers pointing you at the various SystemConfiguration APIs. – bdash Apr 11 '13 at 01:16
  • Oh, sorry! I miss your first link! I will try Reachability right now. – Andrew Chang Apr 11 '13 at 03:01
  • Thanks a lot. I put my further homework above. What about your opinion? – Andrew Chang Apr 11 '13 at 07:27
  • I noticed that this question is marked duplicate. The duplicated target is WRONG! If truly duplicated, it would be this: http://stackoverflow.com/questions/5662298/how-to-check-network-status-in-iphone-app – Andrew Chang Apr 11 '13 at 08:23

1 Answers1

1

You should look at SCNetworkReachability. In general you will create reachability object and add it to run loop and you'll get callback when it will be reachable. See this post for details.
Also take a look at SCNetworkConfiguration: you can take a walk through all interfaces in system and see if any of them is connected.
And also you can take a look at SCDynamicStore. There is an example in apple mailing list.

Community
  • 1
  • 1
cody
  • 3,233
  • 1
  • 22
  • 25
  • I do not need something that complex. What I want to know is if there is ANY network available. For example, a ethernet card is connected to a hub, which cause the network card be the status "linked", and this repersents an available network. – Andrew Chang Apr 10 '13 at 12:11
  • take a look at the second answer in the link I gave you: http://stackoverflow.com/questions/7627058/how-to-determine-internet-connection-in-cocoa – cody Apr 10 '13 at 12:17
  • In general I don't know simple way to do this. There is only complex or not so complex :) – cody Apr 10 '13 at 12:19
  • Haha, maybe you're right. I wonder if there is a general way in UNIX-like system. – Andrew Chang Apr 10 '13 at 12:39
  • ping through exec and check the result? :) – cody Apr 10 '13 at 12:52
  • Haha, ping who? I put my further homework above. What about your opinion? – Andrew Chang Apr 11 '13 at 07:28
  • I don't think that would work. I'd rather use kSCNetworkReachabilityFlagsReachable flag, but you are trying to check null address. Do you know what should it return? About your exclamation marks - it is safe to release it here SCNetworkReachabilityFlags is just a uint32_t and they already filled in. About return value - I'd rather return just comparison result. And what was you asking for?) – cody Apr 11 '13 at 07:44
  • I am trying to check null address, or, ANY address. Which means I do not know the target address of any devices. I simply want to check whether there is at lease one available network connection present in local computer. Hmmm... I am afraid I did not describe my request clearly. My falut. – Andrew Chang Apr 11 '13 at 08:11