Checking Network Availability - iphone

I use reacheability class for checking my network connectivity in my app...
Reachability *reach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [reach currentReachabilityStatus];
if (netStatus==NotReachable)
{
NSLog(#"NR");
}
I need to find when the network status change (i.e when the network status changes from reachable to notreachable and vice versa).
Is there any delegates to find this thinks, Any suggestions ?

I suggest to make use of Apple's Reachability class. Here is a sample App by Apple.
and also check this links.
http://www.switchonthecode.com/tutorials/iphone-snippet-detecting-network-status

Used this flag kReachabilityChangedNotification to find the change in network status and passed that to a NSNotificationCenter
Here is the code:
NSString *host = #"https://www.apple.com"; // Put your host here
// Set up host reach property
hostReach = [Reachability reachabilityWithHostname:host];
// Enable the status notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
[hostReach startNotifier];
- (void) reachabilityChanged: (NSNotification* )note
{
Reachability *reachability = [note object];
NSParameterAssert([reachability isKindOfClass:[Reachability class]]);
if (reachability == hostReach) {
Reachability *reach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [reach currentReachabilityStatus];
if (netStatus==NotReachable)
{
NSLog(#"notreacheable");
}
else {
NSLog(#"reacheable");
[[NSNotificationCenter defaultCenter]postNotificationName:#"startUpdatingTable" object:nil];
}
}
}

use Reachability" class
add flooding method in app delegate so you can use this method any ware in project
#import "Reachability.h"
-(BOOL)isHostAvailable
{
//return NO; // force for offline testing
Reachability *hostReach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [hostReach currentReachabilityStatus];
return !(netStatus == NotReachable);
}

Related

[NSConcreteNotification isReachable]: unrecognized selector sent to instance

I'm trying to get ASIReachability to work in my app which works when the connection isn't there but when the connection exists it give the following error:
2013-04-08 12:26:20.501 Your Llanelli Companion[1576:207] -[NSConcreteNotification isReachable]: unrecognized selector sent to instance 0x7d84d30
I can't seem to remedy this and it's beginning to bug me.
.m file:
- (void) CheckIfAInternetConnectionExists
{
Reachability *reach = [[Reachability reachabilityWithHostName:#"http://176.31.101.181:8020/listen.pls"]retain ];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
[reach startNotifier];
}
- (void) reachabilityChanged:(Reachability *)reach {
if ([reach isReachable ]) {
NSLog(#"connection");
ServerFound = YES;
[self PrepareStream];
} else{
NSLog(#"no connection");
ServerFound = NO;
[self PrepareStream];
}
}
If someone can figure this out for me I'd be very grateful.
When you receive the notification, the parameter to your reachabilityChanged: method will be the notification itself. To get the Reachability object, you'll need to get it from the notification by sending -[NSNotification object] to the notification.
So something like this should work:
- (void) reachabilityChanged:(NSNotification *)note {
Reachability *reach = [note object];
if ([reach isReachable ]) {
NSLog(#"connection");
ServerFound = YES;
} else{
NSLog(#"no connection");
ServerFound = NO;
}
[self PrepareStream];
}

Which is the simplest way to check for Internet connection in iOS? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Check for internet connection - iOS SDK
I'm searching the fastest and simplest way for check the connection in iOS.
I've found this:
-(BOOL)connectedToNetwork {
NSURL* url = [[NSURL alloc] initWithString:#"http://google.com/"];
NSData* data = [NSData dataWithContentsOfURL:url];
if (data != nil)
return YES;
return NO;
}
Do you know if is there something even simpler???
Guys, thanks for all the answers, but I'm searching for the simplest, lightest, solution, not for the best one (i.e. distinction between 3G/Wi-Fi is not needed, I'm only searching a YES/NO reach for a website)
Take a look at the Reachability Example provided by Apple.
The problem your approach may have is that you could have a timeout and thus, the synchronized download of some data may block your app. As a result Apple may reject your app.
The Reachability Example can be used as follows:
Reachability *_reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus remoteHostStatus = [_reachability currentReachabilityStatus];
if (remoteHostStatus == NotReachable) {
// not reachable
} else if (remoteHostStatus == ReachableViaWiFi) {
// reachable via Wifi
} else if (remoteHostStatus == ReachableViaWWAN) {
// reachable via WWAN
}
I suggest dont go with this approach. I faced a rejection of one of my app because of this code. Instead go with Apple's Reachability Classes.
Use this code to check whether the device is connected to internet or not
use this code in viewDidLoad :
Reachability* internetReachable; = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];
hostReachable = [Reachability reachabilityWithHostName: #"www.apple.com"] ;
[hostReachable startNotifier];
and add this function to your code:
-(void) checkNetworkStatus:(NSNotification *)notice
{
recheabilityBool=FALSE;
nonrecheabilityBool=FALSE;
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
nonrecheabilityBool=TRUE;
internetCon=0;
//NSLog(#"The internet is down.");
break;
}
case ReachableViaWiFi:
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
internetCon=404;
[prefs setInteger:internetCon forKey:#"conKey"];
//NSLog(#"The internet is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
//NSLog(#"The internet is working via WWAN.");
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
internetCon=0;
if( nonrecheabilityBool==FALSE)
{
//NSLog(#"A gateway to the host server is down.");
}
break;
}
case ReachableViaWiFi:
{
if(recheabilityBool==FALSE)
{
recheabilityBool=TRUE;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
internetCon=404;
[prefs setInteger:internetCon forKey:#"conKey"];
//NSLog(#"The internet is working via WIFI.");
break;
}
//NSLog(#"A gateway to the host server is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
//NSLog(#"A gateway to the host server is working via WWAN.");
break;
}
}
}
- (BOOL)connected
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
Reachability* reachability = [Reachability sharedReachability];
[reachability setHostName:#"www.example.com"]; // set your host name here
NetworkStatus remoteHostStatus = [reachability remoteHostStatus];
How to check for an active Internet connection on iOS or OSX?
As #who9vy said use Reachability Example
Import the Two classes Reachability.h and Reachability.m into your project
Use method to check the Internet Connection
- (BOOL)isConnectedToInternet
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
The best way to check reachability is Apple Rechability class
Check this link
Hope it helps you..

Reachability method (startnotifier) is not calling

Here is my reference code. I have put break point in startnotifier method but it is not being called.
[[NSNotificationCenter defaultCenter] addObserver: self selector: #selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
internetReach = [[Reachability reachabilityForInternetConnection] retain];
[internetReach startNotifier];
I have written this part of code in appdelegate.m (didFinishLaunchingWithOptions).
I have declared var in appdelegate.h like below....
#interface AppDelegate : UIResponder < UIApplicationDelegate >
{
Reachability *internetReach;
Reachability *wifiReach;
Reachability *hostReach;
}
Why breakpoint in startnotifier is not being called and hence nsnotification is not calling observer function if I change the network.
+(BOOL)ConnectedToNetWork
{
Reachability *HostReach = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [HostReach currentReachabilityStatus];
bool result = false;
if (internetStatus == ReachableViaWiFi)
result = true;
else if(internetStatus==ReachableViaWWAN)
result = true;
return result;
}
use this method whre ever u want to check connection

How often does the iPhone 4 search for data service once it has been lost?

Long time reader, first time asker.
I'm programming an iPhone application that needs to handle the phone going in and out of data coverage with some elegance. I can set up the Reachability with notifications to find out when it gets lost or comes back, but it would be helpful for me to know how often the radios are looking for a signal - and does this rate slow down over time? Also, is there anything I can do programmatically (like pinging a server when I know I don't have coverage) to speed it up?
Battery life is not really a big concern for me, and I will not be deploying through iTunes.
What you want is possible. First off get Reachability code from Apple. Then you need to write a checkNetworkStatus implementation. This is where notifications come -
#import "Reachability.h"
- (void)checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch(internetStatus)
{
case NotReachable:
{
self.internetActive = NO;
break;
}
case ReachableViaWiFi:
{
self.internetActive = YES;
break;
}
case ReachableViaWWAN:
{
self.internetActive = YES;
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
self.hostActive = NO;
break;
}
case ReachableViaWiFi:
{
self.hostActive = YES;
break;
}
case ReachableViaWWAN:
{
self.hostActive = YES;
break;
}
}
return;
}
Now you need to start your notifications -
-(void)viewWillAppear:(BOOL)animated
{
//NSLog(#"View Will Appeared!!");
// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(checkNetworkStatus:)
name:kReachabilityChangedNotification
object:nil];
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName: #"www.google.com"] retain];
[hostReachable startNotifier];
// now patiently wait for the notification
return;
}

iphone:How to check each & every time internet connection in iphone?

Hello friends,
I want to check internet connection each & every time is active or not in my iPhone apps so please tell me any link or any idea to develop this functionality.
Thanks in advance..
Use Reachability Classes
+ (BOOL)isNetworkAvailable {
Reachability *internetReach;
internetReach = [Reachability reachabilityForInternetConnection];
[internetReach startNotifier];
NetworkStatus netStatus = [internetReach currentReachabilityStatus];
if(netStatus == NotReachable) {
NSLog(#"Network Unavailable");
return NO;
}
else
return YES;
}
//check for internet connection
NSStringEncoding enc;
NSError *error;
NSString *connected = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://www.apple.com"] usedEncoding:&enc error:&error];
if (connected == nil) {
//no internet connection..display alert
} else {
//call your function
}
This should help you.
You can use the Reachability example from apple developer.
I use it like this:
-(BOOL) isInternetReachable
{
Reachability *r = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if(internetStatus == NotReachable)
{
return NO;
}
return YES;
}
You can also check to see if a certain server is up with the provided methods.
With the help of Reachability Class and also go through...