3

I am working on iOS application and I would need to detect when network changes either from Wifi connection to another Wifi connection or between Wifi and 3G.

I have tried using Reachability library but it seems it does not detect changes between Wifi connections. What can I use?

Target of the application would be App Store so I can't use private methods of Apple.

UPDATE: After some testing I have found out that when testing using simulator it works perfectly. I get notifications without any problem. iphone problem, maybe?

Thanks in advance

RuLoViC
  • 825
  • 7
  • 23
  • 1
    https://stackoverflow.com/questions/17652465/capture-wi-fi-network-changing-event-in-ios – KethanKumar Aug 30 '17 at 11:54
  • 1
    Possible duplicate of [Capture Wi-Fi network changing event in iOS](https://stackoverflow.com/questions/17652465/capture-wi-fi-network-changing-event-in-ios) – gvuksic Aug 30 '17 at 12:01
  • 1
    I saw that already, but isn´t there another way apart from just setting timer and checking SSID. Then change will not be detected automatically – RuLoViC Aug 30 '17 at 12:22
  • Hi @RuLoViC, Did you find the solution to this problem?. I am also facing the same issue for quite some time. Any help would be appreciated – sanyam jain May 09 '22 at 13:37

2 Answers2

1

Please refer this link https://stackoverflow.com/a/19256197/1382157

Other way,

- (BOOL)isReachable {
return [self isReachableViaWWAN] || [self isReachableViaWiFi];
}

- (BOOL)isReachableViaWWAN {// If this return true, means it is connected to 3g
return self.networkReachabilityStatus == 
AFNetworkReachabilityStatusReachableViaWWAN;
}

- (BOOL)isReachableViaWiFi { // If this return true, means it is connected to wifi
return self.networkReachabilityStatus == 
AFNetworkReachabilityStatusReachableViaWiFi;
}

make sure you initialize class properly and do

[self.manager.reachabilityManager startMonitoring]; 
Sakshi
  • 1,060
  • 11
  • 25
0

please see first Reachibility

After Importing class write in .h

 Reachability* reachability;

.m class

 [[NSNotificationCenter defaultCenter] addObserver:self 
 selector:@selector(handleNetworkChange:) name:kReachabilityChangedNotification object:nil]; 
  reachability = [Reachability reachabilityForInternetConnection]; 
 [reachability startNotifier]; 
 NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus]; 
 if(remoteHostStatus == NotReachable) 
 {
NSLog(@"no");
 } 
 else if (remoteHostStatus == ReachableViaWiFiNetwork) 
 {
NSLog(@"wifi"); 
} 
else if (remoteHostStatus == ReachableViaCarrierDataNetwork) 
{
NSLog(@"cell"); 
} 
..... 

 - (void) handleNetworkChange:(NSNotification *)notice 
 {   
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];   
if(remoteHostStatus == NotReachable) 
{
    NSLog(@"no");
}   
else if (remoteHostStatus == ReachableViaWiFiNetwork) 
{
    NSLog(@"wifi"); 
}   
else if (remoteHostStatus == ReachableViaCarrierDataNetwork) 
{
    NSLog(@"cell"); 
} 
} 
Vikas Rajput
  • 1,754
  • 1
  • 14
  • 26