Reachability ForLocalWiFi - ios

I have downloaded Reachability Class from the developer's portal. On network change this is not working. Below is my code.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
self.wifiReachability = [Reachability reachabilityForLocalWiFi];
[self.wifiReachability startNotifier];
[self updateInterfaceWithReachability:self.wifiReachability];
- (void) reachabilityChanged:(NSNotification *)note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
[self updateInterfaceWithReachability:curReach];
}
Above notificaion is not calling on network change. Any suggestions?

Related

Internet Connection called two Time

I want to call a method when internet is active . But my method is call but it execute two time. I am unable how it is hapenning. Please help .Here is my code.
Reachability * reachability;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleNetworkChanged:) name:kReachabilityChangedNotification object:nil];
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
[self handleNetworkChanged:nil];
}
- (BOOL)handleNetworkChanged:(NSNotification *) notice
{
reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
if(remoteHostStatus == NotReachable) {
self.internetConnection = FALSE;
[[NSNotificationCenter defaultCenter] postNotificationName:#"NetworkGone" object:nil];
//NSLog(#"Internet is not Connected");
} else {
self.internetConnection = TRUE;
[[NSNotificationCenter defaultCenter] postNotificationName:#"NetworkCome" object:nil];
//NSLog(#"Internet is Connected");
}
return self.internetConnection;
}
Remove [self handleNetworkChanged:nil] from applicationDidBecomeActive.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleNetworkChanged:) name:kReachabilityChangedNotification object:nil];
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (BOOL)handleNetworkChanged:(NSNotification *) notice
{
reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
if(remoteHostStatus == NotReachable) {
self.internetConnection = FALSE;
[[NSNotificationCenter defaultCenter] postNotificationName:#"NetworkGone" object:nil];
//NSLog(#"Internet is not Connected");
} else {
self.internetConnection = TRUE;
[[NSNotificationCenter defaultCenter] postNotificationName:#"NetworkCome" object:nil];
//NSLog(#"Internet is Connected");
}
return self.internetConnection;
}

Notification when WiFi connection available

I need a notification when device has WiFi connection is available or Device get connect via WiFi. I need to do some stuff only when WiFi is available.
I have used following code from Reachability:
BOOL status=true;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachability = [Reachability reachabilityForInternetConnection];
[internetReachability startNotifier];
NetworkStatus internetNetworkStatus = [internetReachability currentReachabilityStatus];
status = (internetNetworkStatus == ReachableViaWiFi);
But checkNetworkStatus: method not called properly and accurately. So, please guide me to solve this problem.
Any help to solve problem must be appreciated.
I hope it will help you in your problem.
-(void) rechabilityInit
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
self.internetConnectionReach = [Reachability reachabilityForInternetConnection];
self.internetConnectionReach.reachableBlock = ^(Reachability * reachability)
{
NSLog(#"%#", reachability.currentReachabilityString);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
dispatch_async(dispatch_get_main_queue(), ^{
// Do stuff here when WIFI is availble
}];
};
self.internetConnectionReach.unreachableBlock = ^(Reachability * reachability)
{
NSLog(#"%#", reachability.currentReachabilityString);
dispatch_async(dispatch_get_main_queue(), ^{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// do some stuff here when WIFI not present
}];
};
[self.internetConnectionReach startNotifier];
}
-(void)reachabilityChanged:(NSNotification*)note
{
Reachability * reach = [note object];
if (reach == self.localWiFiReach)
{
if([reach isReachable])
{
NSString * temp = [NSString stringWithFormat:#"LocalWIFI Notification Says Reachable(%#)", reach.currentReachabilityString];
NSLog(#"%#", temp);
}
else
{
NSString * temp = [NSString stringWithFormat:#"LocalWIFI Notification Says Unreachable(%#)", reach.currentReachabilityString];
NSLog(#"%#", temp);
}
}
}
The following way is help me to solve my problem:
// Use below to any method
// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachability = [Reachability reachabilityForInternetConnection];
[internetReachability startNotifier];
-(void) checkNetworkStatus:(NSNotification *)notice {
// called after network status changes
NetworkStatus internetStatus = [internetReachability currentReachabilityStatus];
switch (internetStatus) {
case NotReachable: {
NSLog(#"The internet is down.");
break;
}
case ReachableViaWiFi: {
NSLog(#"The internet is working via WIFI.");
//Remove Observer
[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
//Write your code
break;
}
case ReachableViaWWAN: {
NSLog(#"The internet is working via WWAN.");
break;
}
}
}

Can i unregister NSNotificationCenter inside AppDelegate.m?

I'm registering for Reachability notification inside my AppDelegate.m by below piece of code.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleNetworkChange:)
name:kReachabilityChangedNotification
object:nil];
reachability = [Reachability reachabilityWithHostName:#"www.google.com"];
[reachability connectionRequired];
[reachability startNotifier];
Can i unregister Reachability notifier inside my AppDelegate.m ?
I am trying to unregister Reachability using below code.
-(void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
try to put your NotificationCenter addObserv Code in to
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleNetworkChange:)
name:kReachabilityChangedNotification
object:nil];
reachability = [Reachability reachabilityWithHostName:#"www.google.com"];
[reachability connectionRequired];
[reachability startNotifier];
}
and remove it from:-
- (void)applicationWillResignActive:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

Reachability : Detect when network is reconnected

I am new to iOS and just started working on it. I am trying to implement network reachability to detect when the network is disconnected and when it gets back by using a third party class. I am able to detect the loss of network but I am not able to detect when the network gets back after it is disconnected. I am using the following condition for checking the disconnection which is working well :
// NSURLConnectionDelegate method: Handle the connection failing
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
if(internetStatus==NotReachable)
{
NSLog(#" Network Disconnected")
}
}
I have downloaded the third party reachability class from this link :https://github.com/tonymillion/Reachability
Can anyone suggest me the way to detect when the network is connected again?
You can simply place a notification in your class like this:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
And then, you can use thiis method to observe when the network connection gets back i.e the connection state changes:
- (void) reachabilityChanged:(NSNotification *)note
{
Reachability* currentReach = [note object];
NSParameterAssert([currentReach isKindOfClass:[Reachability class]]);
if (internetStatus != NotReachable)
{
// handle UI as per your requirement
}
}
You called
[reachability startNotifier];
it means that each time reachabilty status is changed it will emit kReachabilityChangedNotification. So what you need now is to subscribe to receive this notification:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(reachabilityStatusChanged:)
name:kReachabilityChangedNotification
object:nil];
and implement reachabilityStatusChanged: method:
- (void)reachabilityStatusChanged:(NSNotification *)notice {
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
if (internetStatus != NotReachable)
{
// do what you need
}
}
Download Reachability.h from this:
https://developer.apple.com/Library/ios/samplecode/Reachability/Introduction/Intro.html
and subscribe to notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

When wi-fi is tuned off, status shows NotReachable but when wi-fi turned on again network status still shows NotReachable

I've this scenario, UIViewController => UITableViewController => UITableViewController => UIViewController.
From first UITableViewController, I check internet connection and populate data based on that.
This is what the problem is -
1. At first UITableViewController wi-fi is on so data populated from URL,
2. I turn-off wi-fi and select row and go to second UITableViewController, network status is NotReachable and data is populated from database
3. I turn on wi-fi and select row and go to last UIViewcontroller, here network status is NotReachable whereas it should be ReachableViaWiFi
Am I missing something? Please suggest.
-(void) viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(reachabilityChanged:)
name: kReachabilityChangedNotification
object: nil];
Reachability *reach = [Reachability reachabilityWithHostname: #"www.apple.com"];
[reach startNotifier];
}
- (void) reachabilityChanged: (NSNotification *)notification {
Reachability *reach = [notification object];
if( [reach isKindOfClass: [Reachability class]]) {
NetworkStatus status = [reach currentReachabilityStatus];
switch(status) {
case NotReachable:
{
if (!performedOnce) {
[self processOffline];
performedOnce = YES;
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
}
break;
default:
{
if (!performedOnce) {
[self retrieveUserListWithUrl];
performedOnce = YES;
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
}
break;
}
}
}

Resources