Reachability working on simulator but not on device - iphone

In my project I am using Reachability class provided by Apple. When there is no internet connection, I am displaying an alert message. Everything is working fine, when I test it on the simulator, but when am running it on iPad, the alert message is not shown when there is no internet.
I am running the code on iOS 5.0.
Any help would be appreciated.
EDIT:
-(BOOL)isInternetConnectionPresent{
Reachability *objReachability = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [objReachability currentReachabilityStatus];
if(internetStatus != NotReachable)
{
return YES;
}
return NO;
}
UPDATE:
Used NSLog to debug. Seems there was some problem with WWAN, even when there was no SIM card. Restarted the iPad & switched OFF & ON the Wi Fi again. Now it works fine. Thanks for the help guys.

You have to check all the NetworkStatus and Cross Check the device Wifi Connection status again
Example:
// to check if, wifi connected properly in current device.
- (BOOL)networkCheck {
Reachability *wifiReach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [wifiReach currentReachabilityStatus];
switch (netStatus)
{
case NotReachable:
{
NSLog(#"NETWORKCHECK: Not Connected");
return NO;
break;
}
case ReachableViaWWAN:
{
NSLog(#"NETWORKCHECK: Connected Via WWAN");
return NO;
break;
}
case ReachableViaWiFi:
{
NSLog(#"NETWORKCHECK: Connected Via WiFi");
return YES;
break;
}
}
return false;
}

In My case the following code snippet is working perfectly with iOS 5. Here i am checking internet connectivity with WIFI.
- (NSString *)stringFromStatus:(NetworkStatus ) status {
NSString *string;
switch(status) {
case NotReachable:
string = #"Not Reachable";
break;
case ReachableViaWiFi:
string = #"Reachable via WiFi";
break;
case ReachableViaWWAN:
string = #"Reachable via WWAN";
break;
default:
string = #"Unknown";
break;
}
return string;
}
------------------------ now with following line of code you can check.
Reachability *reach =[Reachability reachabilityForLocalWiFi] ;
NetworkStatus status = [reach currentReachabilityStatus];
NSLog(#"%#", [self stringFromStatus: status]);
if ([[self stringFromStatus: status] isEqualToString: #"Not Reachable"])
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Connection Failure !"
message:#"Your Device is not Connected to any active WIFI Connection."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
else
{ //connected to internet.
}

Related

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..

Background network checking

I have an app that displays the network availability whenever there is some activity such as the view change. However i am looking for a code that runs in the background to check the network availability even if i will be idle on the same screen and it must display a message that "network is not available/network is available"
I use this piece of code to detect network availability. I basically declare local vars to figure out the things like whether I am on WiFi or 3G network or whether internet is available.
Notifications come at some intervals & update these variables. You access these BOOL vars to know the status.
- (void)checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch(internetStatus)
{
case NotReachable:
{
//NSLog(#"The internet is down.");
self.internetActive = NO;
break;
}
case ReachableViaWiFi:
{
//NSLog(#"The internet is working via WIFI.");
self.internetActive = YES;
break;
}
case ReachableViaWWAN:
{
//NSLog(#"The internet is working via WWAN.");
self.internetActive = YES;
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
//NSLog(#"A gateway to the host server is down.");
self.hostActive = NO;
break;
}
case ReachableViaWiFi:
{
//NSLog(#"A gateway to the host server is working via WIFI.");
self.hostActive = YES;
break;
}
case ReachableViaWWAN:
{
//NSLog(#"A gateway to the host server is working via WWAN.");
self.hostActive = YES;
break;
}
}
return;
}

How To check if wifi option enabled or not

How to check if wifi option is enabled on the iPhone or not (but maybe iPhone not connected to one of the wifi net).
For this you need to import reachability classes in your project.
After then:-
#import "Reachability.h"
In you view DidLoad write:-
- (void)viewDidLoad {
Reachability *internetReach = [[Reachability reachabilityForInternetConnection] retain];
[internetReach startNotifer];
Reachability *wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
[wifiReach startNotifer];
NetworkStatus netStatus1 = [internetReach currentReachabilityStatus];
NetworkStatus netStatus2 = [wifiReach currentReachabilityStatus];
if(netStatus1 == NotReachable && netStatus2 == NotReachable)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry" message:#"This feature requires an internet connection." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
else
{//wifi connection available;
}
}
Found a great line of code for this.
Add the Reachability class to your project and then you can do this:
BOOL isConnectedProperly = ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == ReachableViaWiFi);
First import Reachability files into your project.
-(void)loginButtonTouched
{
bool success = false;
const char *host_name = [#"www.google.com"
cStringUsingEncoding:NSASCIIStringEncoding];
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName
(NULL, host_name);
SCNetworkReachabilityFlags flags;
success = SCNetworkReachabilityGetFlags(reachability, &flags);
bool isAvailable = success && (flags & kSCNetworkFlagsReachable) &&
!(flags & kSCNetworkFlagsConnectionRequired);
if (isAvailable)
{
NSLog(#"Host is reachable: %d", flags);
// Perform Action if Wifi is reachable and Internet Connectivity is present
}
else
{
NSLog(#"Host is unreachable");
// Perform Action if Wifi is reachable and Internet Connectivity is not present
}
}
When loginButtonTouched method is called we check that www.google.com is reachable or not.
SCNetworkReachabilityFlags returns flags which helps us to understand the Status of internet connectivity.
If isAvailable variable returns "true" then Host is
Reachable means Wifi is reachable and Internet Connectivity is present.

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

How do I receive notifications that the connection has changed type (3G, Edge, Wifi, GPRS)

Is there a way to discover whether the current connection is 3G, Edge or WiFi, and receive a notification of this? Or just a way do discover whether the connection is WiFi?
You should use Apple's Reachability class
http://developer.apple.com/library/ios/#samplecode/Reachability/Listings/Classes_Reachability_h.html
after implementation you can use this in the delegate:
- (void) configureTextField: (Reachability*) curReach
{
NetworkStatus netStatus = [curReach currentReachabilityStatus];
BOOL connectionRequired= [curReach connectionRequired];
NSString* statusString= #"non";
switch (netStatus)
{
case NotReachable:
{
statusString = #"Access Not Available";
//Minor interface detail- connectionRequired may return yes, even when the host is unreachable. We cover that up here...
connectionRequired= NO;
break;
}
case ReachableViaWWAN:
{
statusString = #"Reachable WWAN";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Cellular Data Detected" message:#"You are using Cellular data such as 3G or Edge. Downloading large amount of data may effect your cellular internet package costs. To avoid such extra cost kindly use Wifi."
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
break;
}
case ReachableViaWiFi:
{
statusString= #"Reachable WiFi";
break;
}
}
if(connectionRequired)
{
statusString= [NSString stringWithFormat: #"%#, Connection Required", statusString];
}
NSLog(#"Network= %#",statusString);
if ([statusString isEqualToString:#"Access Not Available"]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No Internet Connection" message:#"It seems you are not connected to the internet, the app will try to load from the last cached data - assuming this data exist."
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
//textField.text= statusString;
}
- (void) updateInterfaceWithReachability: (Reachability*) curReach
{
if(curReach == hostReach)
{
//[self configureTextField: remoteHostStatusField imageView: remoteHostIcon reachability: curReach];
// NetworkStatus netStatus = [curReach currentReachabilityStatus];
// BOOL connectionRequired= [curReach connectionRequired];
[self configureTextField:curReach];
}
}
Yes, Apple has an example project that shows that. Reachability
And here's an example.