2

I want to check connection to a local wifi with bonjour using [Reachability reachabilityForLocalWifi] but it is now deprecated in iOS 10. Apple suggested to use [Reachability reachabilityWithAddress:] but it still returns "not reachable" even when i'm connected to a local wifi.

update: Here is my code using reachabilityWithAddress which always returns "not reachable" for local wifi.

+(BOOL)checkWifiConnectionIfOn{
    BOOL isOn;

    struct sockaddr_in localWifiAddress;
    bzero(&localWifiAddress, sizeof(localWifiAddress));
    localWifiAddress.sin_len = sizeof(localWifiAddress);
    localWifiAddress.sin_family = AF_INET;

    // IN_LINKLOCALNETNUM is defined in <netinet/in.h> as 169.254.0.0.
    localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);

    Reachability* returnValue = [Reachability reachabilityWithAddress:(struct sockaddr_in *) &localWifiAddress];
    if (returnValue != NULL && [returnValue currentReachabilityStatus] != NotReachable) //Reachable
    {
        //
        isOn = YES;
    }
    else{ // Not Reachable
        NSString *log = [NSString stringWithFormat:@"WifiController checkWifiConnectionIfOn Wifi OFF"];
        [Logger writeDeviceLog:log logType:ERROR_LOG];

        isOn = NO;

    }
    return isOn;
}
binsnoel
  • 276
  • 2
  • 17
  • [have you checked here ?](http://stackoverflow.com/questions/37742367/reachabilitywithaddress-is-not-working-in-objective-c-programming). – vaibhav Oct 14 '16 at 07:50
  • @vaibhav i have. i have updated my question to include my code. – binsnoel Oct 14 '16 at 08:08
  • try turning off the wifi and data then try. – vaibhav Oct 14 '16 at 08:18
  • I tried turning off wifi and data, It responds `Not Reachable`. however, what i want is when I am connected to a local wifi (not internet), my method `checkWifiConnectionIfOn` should return `Reachable` – binsnoel Oct 14 '16 at 09:04
  • I ended up changing the requirement for this. have not found any workaround that works for me. – binsnoel Mar 10 '17 at 03:29

0 Answers0