Let me first stress the fact that I’m talking about the Mac OS X SDK, not iPhone.
In order to determine the “connectivity” 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);
Which is fine for just that — getting info on the reachability of Google (which is exactly what I want to know).
Is there a way to add an observer to the changes? I’ve been looking into SCDynamicStore, but I find the single example from Apple and the documentation a bit overwhelming.
Ideally I’d want to be able to set a function for flag changes, but this will suffice: notice when IP is “dropped”/released, and when it is obtained. (I could then do the reachability hardcoded in the function which is triggered on IP obtained).
Please don’t hesitate to ask for elaborations.
Yes, you can use
SCNetworkReachabilitySetCallbackandSCNetworkReachabilityScheduleWithRunLoop. You don’t need to useSCDynamicStoreunless you want to specifically watch a particular network interface.If you want to take a look at a complete example, you can see what I did for NCIDpop (a network caller ID displayer). Search for
SCNetworkReachabilityin this file. The comments in thenetworkReachabilityCallbackfunction give you some idea of what state transitions to expect (which weren’t terribly well documented when I wrote that code).