General
iOS

Reachability – iOS

Vishnu kanth

03 Nov 2016

Reachability - iOS

Obstacle

How many of you guys facing problems with Reachability Library for checking whether Wi-Fi Connection (reachabilityForLocalWiFi)is available or not? Even i faced the same problem in detecting Wi-Fi connection, where network change notifications won’t come properly when we use reachabilityForLocalWiFi for iOS application development. Apple has updated the Library with IPV6 support, but still there is a problem in detecting only Wi-Fi connection if the device(Mobile) cannot reach the DNS server(host) with in 30 sec. May be some one (like me) in this world has a requirement to check only Wi-Fi (local Wi-Fi without a connection to the internet), for those here is the solution…

Way Out

Enabled Network notifications variable to YES, to look for change in the Network constantly

//Make NetworkStatusNotifications YES, to get notifications and add the query in run loop
 
 [[Reachability sharedReachability]setNetworkStatusNotificationsEnabled:YES];
 
 
 
 
 
 ReachabilityQuery *query = [[ReachabilityQuery alloc]init];
 
 [query scheduleOnRunLoop:[NSRunLoop mainRunLoop]];
 
  
 
 //Registering network change notifications
 
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:@"kNetworkReachabilityChangedNotification" object:nil];
 
 
 
 [self reachabilityChanged:nil];
 
 
 
 //This is the method where we regularly looking for network change
 
 - (void) reachabilityChanged:(NSNotification *)note
 
 {
 
  if ([[Reachability sharedReachability] localWiFiConnectionStatus] == ReachableViaWiFiNetwork )
 
  {
 
  //Continue to do our operations
 
  }
 
  else
 
  {
 
  //Before showing Turn on Wi-Fi alert, we will check whether the SSID is really NONE or not
 
  if(![[self getCuurentSSID] isEqualToString:@"<>"])
 
  {
 
  //Then we will continue with our operations
 
  }
 
  else
 
  {
 
  //Here we will show Wi-Fi alert pop up to the user
 
  }
 
  }
 
 
 
 }
 
 
 
 
 
 //Here is the method to fetch Current SSID(Network) name connected
 
 -(NSString*)getCuurentSSID
 
 {
 
  NSString *currentSSID = @"";
 
  CFArrayRef myArray = CNCopySupportedInterfaces();
 
  if (myArray != nil){
 
  NSDictionary* myDict = (NSDictionary *) CFBridgingRelease(CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0)));
 
  if (myDict!=nil){
 
  currentSSID=[myDict valueForKey:@"SSID"];
 
  } else {
 
  currentSSID=@"<>";
 
  }
 
  } else {
 
  currentSSID=@"<>";
 
  }
 
  return currentSSID;
 
 }

Hope this solution will help those who are actually depending on only Wi-Fi connection. For more information please visit Reachability.

For Mobile Application Development services contact Cumulations Technologies.

Related read: Using both Ethernet and Wifi interfaces for Communication in AndroidThings