tags:

views:

33

answers:

1

Hi all,

My application needs continuous network connectivity. I'm currently using Apple's "Reachability" class to check reachability at the start of the app.

I need to poll the network in order to check its functionality. What's the good practice to achieve this?

+1  A: 

You shouldn't need to poll if you're using the Reachability class. You should be able to set a callback / delegate method on the reachability monitor and get notified when reachability changes.

Something like this:

SCNetworkReachabilitySetCallback(reachability, networkChangedListener, &context);

where 'networkChangedListener` is your callback function. This will be called whenever reachability changes, and from there you can post a notification to let interested parties know.

Jasarien
Thanx Jasarien.. it helped..
neha
I have a doubt: Reachability seems to be unreliable as sometimes it displays the network message or just show up a black screen. So do we have a better way? NSUrlConnection seems to be a better option but then what about polling network in that case? Will I need to run a background thread? This seems to be an expensive option. What would be the good practice in this case?
neha
I haven't had any experiences where Reachability has been unreliable. Do you see it being unreliable in terms of incorrect notifications about network changes or incorrect information on the initial reachability check?
Jasarien
Reachability callbacks will only tell you if the first hop has been broken. If someone pulls the cable on the other side of your wifi access point, reachability may happily tell you that the network is still reachable. This level of connectivity reporting isn't useful for some application types.
hotpaw2
I don't believe that's strictly true. Reachability allows you to set a remote host and check the reachability of that host in addition to checking whether the local wifi/network is available.
Jasarien
@Jasarien I tried using this method but getting an error at "networkChangedListener". Can you please guide, I have done it as follows: struct sockaddr_in zeroAddr;bzero(zeroAddr.sin_len = sizeof(zeroAddr);zeroAddr.sin_family = AF_INET;SCNetworkReachabilityRef target = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *) SCNetworkReachabilityFlags flags;SCNetworkReachabilityGetFlags(target, SCNetworkReachabilityCallBack(target, flags, networkChangedListener); I'm getting an error "expected ")" before ',' token" at last line.
neha
`networkChangedLister` is the name of a function that will be called when the network connection changes. You either need to define that function, or create your own and pass it into `SCNetworkReachabilityCallBack()`
Jasarien