xcode - How to check if any network available in OS X app? -
this question has answer here:
in app, wanted check if there type of existing network available in current os x system. ethernet or internet or wifi or 3g-card or other type of network communication.
how can achieve that?
thank you!
https://stackoverflow.com/questions/5662298...
thank bdash , cody! homework , found answer above. iphone, make 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 "kscnetworkflagsislocaladdress" only, not sure if ok.
i tried flag when remove network connection , returned 0x07.
you should @ scnetworkreachability. in general create reachability object , add run loop , you'll callback when reachable. see this post details.
take @ scnetworkconfiguration: can take walk through interfaces in system , see if of them connected.
, can take @ scdynamicstore. there example in apple mailing list.
Comments
Post a Comment