1

I am new to ios development. I want callback when connectivity change (or ip address of device is changed). I have added Reachability (sample code of Apple) in my application. But it does not give callback.

I have done same thing in Android - receiving "ConnectivityManager.CONNECTIVITY_ACTION" which detects a change in network connectivity has occurred.

Is there any API available in iOS which can do similar functionality as CONNECTIVITY_ACTION ? Any help is appreciated. Thanks...

1 Answer 1

0

Use SCNetworkReachabilitySetCallback:

Boolean SCNetworkReachabilitySetCallback (
   SCNetworkReachabilityRef target,
   SCNetworkReachabilityCallBack callout,
   SCNetworkReachabilityContext *context
);

So your callback method will look like this

typedef void (*SCNetworkReachabilityCallBack) (
   SCNetworkReachabilityRef target,
   SCNetworkReachabilityFlags flags,
   void *info
);

meaning you first declare the callback

static void MyReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info)
{
 // do somthing
}

and then register it:

SCNetworkReachabilityContext ctx = {0, self, NULL, NULL, NULL};
if(SCNetworkReachabilitySetCallback(reachabilityRef, MyReachabilityCallback, &ctx)) {
 // registered callback
2
  • This is Reachability code, which I have already implemented. But "MyReachabilityCallback" is not called every times when I change connectivity. Apr 10, 2013 at 4:15
  • As I said Rechability code is already added to my application. But I have notice that when app goes in background and after some time it goes in suspended state. Not every time but it is random. When app is suspended, callback is not received, but I guess this reason for its behavior.Anyway thanks a lot for help. May 29, 2013 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.