Background network checking - iphone

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

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

Reachability working on simulator but not on device

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

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

iPhone SDK reachabilityWithAddress issue

I need to check connectivity with IP address only. I'm trying to do it with Apple Reachability class, using reachabilityWithAddress option. And my problem is I can put any IP address in callAddress.sin_addr.s_addr field and statusText always will be "reachble". How can I do to exactly check connectivity to IP address?
-(BOOL)reachabilityTest {
struct sockaddr_in callAddress;
callAddress.sin_len = sizeof(callAddress);
callAddress.sin_family = AF_INET;
callAddress.sin_port = htons(24);
callAddress.sin_addr.s_addr = inet_addr("212.83.3.190");
Reachability *hostReach = [[Reachability reachabilityWithAddress:&callAddress] retain];
NetworkStatus netStatus = [hostReach currentReachabilityStatus];
if (netStatus == NotReachable)
{
NSLog(#"NotReachable");
statusField.text = #"NOT reachable";
return NO;
}
if (netStatus == ReachableViaWiFi)
{
NSLog(#"ReachableViaWiFi");
statusField.text = #"reachable";
return YES;
}
[Reachability release];
}
Instead of using the Reachability classes, go old-school and open a TCP connection to it. If the response to connect() is EHOSTUNREACH, then you can't. If the response is ECONNREFUSED, or a successful connection, then you can.