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;
}
}
}
Related
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?
I want my app to detect the internet connection loss automatically. So im using the following code.
- (void)applicationDidBecomeActive:(UIApplication *)application {
Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
if (networkStatus == NotReachable) {
[Settings hideSpinner];
//Show no internet connectivity dialog.
} else {
}
}
But the problem is that it is not checking the internet connectivity continually.
it checks only when the app has become active. How can I be able to check for internet connection continually throughout the app life cycle and throw an warning if internet goes off automatically?
Once your application has launched, you can fire a NSTimer to do the same:
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:#selector(checkForConnectivity)
userInfo:nil
repeats:YES];
}
-(void)checkForConnectivity {
Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
if (networkStatus == NotReachable) {
//No internet connectivity - perform required action
}
else {
//Internet connectivity is valid
}
}
Thanks!
Add obeserver like this in Reachability method.
1) [[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
It will call automatically when your app open/in background mode and it call reachabilityChanged.
2) [[NSNotificationCenter defaultCenter] postNotificationName:#"ChangeInternetConnection" object:nil];
ChangeInternetConnection you have to add observer to your ViewController to get status when internet changing it's status.
- (void) checkInternetConnetion {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
//NSString *remoteHostName = #"www.apple.com";
self.internetReachability = [Reachability reachabilityForInternetConnection];
[self.internetReachability startNotifier];
[self updateInterfaceWithReachability:self.internetReachability];
}
#pragma mark - Reachability Methods
- (void)updateInterfaceWithReachability:(Reachability *)reachability {
if (reachability == self.internetReachability) {
[self checkStatus:reachability];
}
if (reachability == self.wifiReachability) {
[self checkStatus:reachability];
}
}
-(void)checkStatus :(Reachability *)reachability {
NetworkStatus netStatus = [reachability currentReachabilityStatus];
BOOL connectionRequired = [reachability connectionRequired];
NSString* statusString = #"";
switch (netStatus) {
case NotReachable: {
self.isInternetOn = FALSE;
break;
}
case ReachableViaWWAN: {
self.isInternetOn = TRUE;
break;
}
case ReachableViaWiFi: {
self.isInternetOn = TRUE;
break;
}
}
[[NSNotificationCenter defaultCenter] postNotificationName:#"ChangeInternetConnection" object:nil];
}
- (void) reachabilityChanged:(NSNotification *)note {
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
[self updateInterfaceWithReachability:curReach];
}
Timer is not an efficient way to do this but you can also use timer too.
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:#selector(handleConnectivity)
userInfo:nil
repeats:YES];
}
-(void)handleConnectivity
{
Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
if (networkStatus == NotReachable)
{
//No internet connectivity - perform required action
}
else
{
//Internet connectivity is valid
}
}
Best way is to use Reachability code. Check here for apple sample code. That has a lot of convenience methods to check internet availability, Wifi/WAN connectivity check etc..
For example:-
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(networkChanged:) name:kReachabilityChangedNotification object:nil];
reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
- (void)networkChanged:(NSNotification *)notification
{
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
if(remoteHostStatus == NotReachable) { NSLog(#"not reachable");}
else if (remoteHostStatus == ReachableViaWiFiNetwork) { NSLog(#"wifi"); }
else if (remoteHostStatus == ReachableViaCarrierDataNetwork) { NSLog(#"carrier"); }
}
You can only check this things in background
*audio—The app plays audible content to the user while in the background. (This content includes streaming audio or video content
using AirPlay.)
*location—The app keeps users informed of their location, even while it is running in the background.
*voip—The app provides the ability for the user to make phone calls using an Internet connection.
*newsstand-content—The app is a Newsstand app that downloads and processes magazine or newspaper content in the background.
*external-accessory—The app works with a hardware accessory that needs to deliver updates on a regular schedule through the External
Accessory framework.
*bluetooth-central—The app works with a Bluetooth accessory that needs to deliver updates on a regular schedule through the Core Bluetooth
framework.
*bluetooth-peripheral—The app supports Bluetooth communication in peripheral mode through the Core Bluetooth framework.
Firstly import in your class: #import "Reachability.h"
Then do like following way:
add an observer for reachability change notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reachabilityChanged:)
name:kReachabilityChangedNotification object:nil];
-(BOOL)reachabilityChanged:(NSNotification*)note
{
BOOL status =YES;
NSLog(#"reachabilityChanged");
Reachability * reach = [note object];
if([reach isReachable])
{
status = YES;
NSLog(#"your network is Available");
}
else
{
status = NO;
//Do something here
}
return status;
}
Add a observer.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
-(BOOL)reachabilityChanged:(NSNotification*)note
{
BOOL status =YES;
NSLog(#"reachability is changed");
Reachability * reach = [note object];
if([reach isReachable])
{
status = YES;
NSLog(#"NetWork is Available. Please go ahead");
}
else
{
status = NO;
NSLog(#"NetWork is not Available. Please check your connection.");
}
return status;
}
You can make use of Reachability framework from iOS and combine it with NSTimer.
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;
}
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];
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;
}
}
}