Receive Online / Offline Availability Notifications

Let me first emphasize the fact that I'm talking about the Mac OS X SDK, not the iPhone.

To define a "connection" and get the flags, I do something similar to:

#import <SystemConfiguration/SystemConfiguration.h>
const char *hostName = [@"google.com" cStringUsingEncoding:NSASCIIStringEncoding];
SCNetworkReachabilityRef target = SCNetworkReachabilityCreateWithName(NULL, hostName);
SCNetworkConnectionFlags flags = 0;
SCNetworkReachabilityGetFlags(target, &flags);

It’s good for this to get Google accessibility information (that’s what I want to know).

Is there a way to add an observer to the changes? I looked SCDynamicStore, but I found a single example from Apple and the documentation is a bit overwhelming.

Ideally, I would like to be able to set a function for flag changes, but that will be enough: pay attention when the IP is “dropped” / released and when it will be received. (Then I could make reachability hard-coded into a function that fires when an IP is received).

Please feel free to ask for development.

+5
source share
2 answers

Yes, you can use SCNetworkReachabilitySetCallbackand SCNetworkReachabilityScheduleWithRunLoop. You do not need to use SCDynamicStoreit if you do not want to specifically monitor a specific network interface.

If you want to take a look at the full example, you can see what I did for NCIDpop (displaying the caller ID of the network). Locate SCNetworkReachabilityin this file . The comments in the function networkReachabilityCallbackgive you some idea of ​​what state expectations to expect (which were not well documented when I wrote this code).

+8
source

Apple Reachability.

  • , , Reachability.
  • ( Objective-C, C)

, SCReachability kReachabilityChangedNotification , :

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];

/ Reachability.h .m (, , , Apple Obj-C !)

. C, SCNetworkReachabilitySetCallback ( Apple) C, . , RunLoop RunLoop,

+3

All Articles