3

I am having one small issue. I am using Reachability class for testing internet connection and it is working perfectly. However suppose if user is using Wifi, but they cannot have access to internet, it should retry for few around 10 seconds, and then it should show an alert to user to switch over there cellular network. Below is my code. I am not getting to point where there is slow network connection when Wifi is On.

self.internetReachable = [Reachability reachabilityForInternetConnection];
[self.internetReachable startNotifier];

//_hasConnectivity = ([self.internetReachable currentReachabilityStatus] != NotReachable);
if([self.internetReachable currentReachabilityStatus] == NotReachable) {
    _hasConnectivity = NO;
}
else if([self.internetReachable currentReachabilityStatus] == ReachableViaWiFi){
    _hasConnectivity = YES;
}
else if([self.internetReachable currentReachabilityStatus] == ReachableViaWWAN) {
    _hasConnectivity = YES;
}
iPatel
  • 46,010
  • 16
  • 115
  • 137
Kashif Jilani
  • 1,207
  • 3
  • 22
  • 49

1 Answers1

6

Try with following code its return BOOL value, Its return YES if connection is available other wise it return NO.

- (BOOL)isNetworkAvailable
{
   CFNetDiagnosticRef dReference;        
   dReference = CFNetDiagnosticCreateWithURL (NULL, (__bridge CFURLRef)[NSURL URLWithString:@"www.apple.com"]);

   CFNetDiagnosticStatus status;
   status = CFNetDiagnosticCopyNetworkStatusPassively (dReference, NULL);        

   CFRelease (dReference);

   if ( status == kCFNetDiagnosticConnectionUp )
   {
      NSLog (@"Connection is Available");
      return YES;
   }
   else 
   {
    NSLog (@"Connection is down");
    return NO;
   }
}
iPatel
  • 46,010
  • 16
  • 115
  • 137
  • 1
    Thanks for quick answer, so it will work in this scenario when user is showing Wifi Connection, however they cannot access to internet due to slowness. – Kashif Jilani Sep 26 '13 at 14:11
  • not working for my case where internet is connected with 3g and it says connection is down. – Majid Bashir Dec 28 '16 at 14:58