RuLoViC
RuLoViC

Reputation: 855

How to detect wifi network change in iOS

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

Upvotes: 3

Views: 4353

Answers (2)

Vikas Rajput
Vikas Rajput

Reputation: 1874

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"); 
} 
} 

Upvotes: 0

Sakshi
Sakshi

Reputation: 1040

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]; 

Upvotes: 1

Related Questions