Search code examples
androidiphoneiosreachability

Need same callback in ios as "ConnectivityManager.CONNECTIVITY_ACTION" in android


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...


Solution

  • 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