Question

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;
}
Was it helpful?

Solution

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;
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top