Getting online/offline reachability notifications
Asked Answered
N

2

5

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.

Neumann answered 25/6, 2011 at 13:52 Comment(0)
R
8

Yes, you can use SCNetworkReachabilitySetCallback and SCNetworkReachabilityScheduleWithRunLoop. You don't need to use SCDynamicStore unless 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 SCNetworkReachability in this file. The comments in the networkReachabilityCallback function give you some idea of what state transitions to expect (which weren't terribly well documented when I wrote that code).

Raynard answered 25/6, 2011 at 13:58 Comment(0)
R
3

Use the Reachability classes from Apple Reachability sample code.

  • It is a good sample to understand how Reachability works
  • It will be much more easier to use (and will be Objective-C and not plain C)

Especially this sample make the SCReachability post a notification named kReachabilityChangedNotification upon reachability changes, so you can add yourself as a listener to this notification easily using:

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

You can copy/paste the Reachability.h & .m files in your project (actually that's what everyone does; I don't know why Apple didn't add this Obj-C classes directly in their frameworks!)

Note: If you still want to use plain C, you can also call SCNetworkReachabilitySetCallback yourself (as what is done in the above Apple sample) and give it a pointer to a C function in which you implement your code. If you do that, don't forget to schedule the reachability on the RunLoop to start the observation process, and remove it from the RunLoop when done

Rowena answered 25/6, 2011 at 13:57 Comment(6)
I've attempted to use Reachability.h/m before, without luck. I tried again, and used the Reachability.h & .m from the link, but I have to do some fixes, is this normal? i.e.: line # 235: if ((flags & kSCNetworkReachabilityFlagsIsDirect) == kSCNetworkReachabilityFlagsIsDirect) and kSCNetworkReachabilityFlagsIsWWAN kSCNetworkReachabilityFlagsIsDirect. Trying to disconnect/reconnect/making the host unavailble in /etc/hosts doesn't push any notifications to my app at the moment.Neumann
No that's not normal. Which version did you try? I mean did you download it recently? The sample has been updated on July 2010 to be compatible with iOS4. In my own project I use a version with copyright 2008 (I should update it!) and don't have anything on line #235...Rowena
Oh, okay. Read the first line of my question again, hehe. I hope they'll add it in Lion, and then use Nicholas' approach until then. But thanks :)Neumann
Right ;) Did you try SimpleReach sample code too?Rowena
Yes, but that's not in light of Nicholas' answer. That's what I'm trying to implement now, thanks :)Neumann
Note on iOS 8 this is delayed by about 5 seconds which is unfortunate for us.Schoenfelder

© 2022 - 2024 — McMap. All rights reserved.