Difference Between internetStatus and hostStatus - ios

i want to check internet connection in my App. While doing that, i use following code.
What is the difference between internetStatus and host status. Which should i use to check whether ipad has internet connection or not.
-(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;
//NSLog(#"A gateway to the host server is down.");
//self.hostActive = NO;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Connection Failed"
message:#"No internet connection"
delegate:nil
cancelButtonTitle:#"Exit"
otherButtonTitles:nil];
[alert setDelegate:self];
[alert show];
[alert release];
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;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Connection Failed"
message:#"No internet connection"
delegate:nil
cancelButtonTitle:#"Exit"
otherButtonTitles:nil];
[alert setDelegate:self];
[alert show];
[alert release];
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;
}
}
}

internetStatus or hostStatus just the instance name you can use any name for status. but specifically if you don't set the host then you can use internetStatus for internet access if it is reachable or not, by default apple checks for internet availability to the internet gateway or internet connection, in this case we don't know the hostname for checking the internet connectivity , however you can use hostStatus or any instance name for a particular host, you could set the random host yourself e.g www.google.com and check if it is reachable. procedure is similar.
basically status is depending on your hostname which can be default or you can set it yourself like this;
hostReachable = [Reachability reachabilityWithHostName: #"www.google.com"];
[hostReachable startNotifier];

Related

AFNetworking returns no connection despite internet connectivity

I'm using AFNetworking to determine whether the user has an internet connection. It returns false on both wifi and 4G LTE and I have checked to make sure they're operational.
I have the following code:
-(void)viewDidLoad{
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
if ([self connected])
[self doSomething];
else
[self noConnection];
}
- (BOOL)connected {
NSLog(#"This returns 0 %hhd", [AFNetworkReachabilityManager sharedManager].reachable);
return [AFNetworkReachabilityManager sharedManager].reachable;
}
-(void)noConnection{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No Internet!"
message:#"Sorry, but you don't have an internet connection at this time. Please try again later."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
This is not a solution using AFNetworking but using sample code from apple
Once you have downloaded and imported Reachbility.m and Reachbility.h files
create a helper function:
-(BOOL)isConnected{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
Then use it
if([self isConnected]){
//connected!
}
else{
//not connected to internet!
}
Very important
If your project is not using arc
go to target >
Build Phase >
double click the Reachability file
add -fno-objc-arc
Hope this helps

Reachability not working

I am currently writing a game which needs an internet connection (TCP to server). I'm attempting to utilize the Reachability class from Apple, but it doesn't work. I'm new to NotificationCenter and Reachability, so please excuse me if this is a stupid question.
Reachability related props in LoginViewController.h
#property (nonatomic) Reachability *hostReachability;
#property (nonatomic) Reachability *internetReachability;
#property (nonatomic) Reachability *wifiReachability;
#property BOOL internetActive;
#property BOOL hostActive;
In the LoginViewController.m
//Reachability
[[NSNotificationCenter defaultCenter] addObserver: self selector: #selector(handleNetworkChange:) name: kReachabilityChangedNotification object: nil];
NSString* hostIP = #"<my server IP is here, removed for obvious reasons>";
hostReachability = [Reachability reachabilityWithHostname:hostIP];
[hostReachability startNotifier];
NetworkStatus hostStatus = [internetReachability currentReachabilityStatus];
if(hostStatus == NotReachable) {NSLog(#"no");}
else if (hostStatus == ReachableViaWiFi) {NSLog(#"wifi"); }
else if (hostStatus == ReachableViaWWAN) {NSLog(#"cell"); }
hostReachability = [Reachability reachabilityForInternetConnection];
[internetReachability startNotifier];
NetworkStatus internetStatus = [internetReachability currentReachabilityStatus];
if(internetStatus == NotReachable) {NSLog(#"no");}
else if (internetStatus == ReachableViaWiFi) {NSLog(#"wifi"); }
else if (internetStatus == ReachableViaWWAN) {NSLog(#"cell"); }
handleNetworkChange: procedure
- (void)handleNetworkChange:(NSNotification*)notification{
NetworkStatus internetStatus = [internetReachability currentReachabilityStatus];
switch (internetStatus) {
case NotReachable:
{
NSLog(#"No internet connection");
self.internetActive = NO;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No internet connection"
message:#"Could not connect to the internet. Please try again later."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
break;
case ReachableViaWiFi:
{
NSLog(#"The internet is working via WIFI.IN AGENDA");
self.internetActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(#"The internet is working via WWAN.IN AGENDA");
self.internetActive = YES;
break;
}
default:
break;
}
NetworkStatus hostStatus = [hostReachability currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
NSLog(#"A gateway to the host server is down.IN AGENDA");
self.hostActive = NO;
NSLog(#"No internet connection");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No connection to host"
message:#"Could not connect to the host server. Please try again later."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
break;
}
case ReachableViaWiFi:
{
NSLog(#"A gateway to the host server is working via WIFI.IN AGENDA");
self.hostActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(#"A gateway to the host server is working via WWAN.IN AGENDA");
self.hostActive = YES;
break;
}
}
When running the application, I always get "no no" in the terminal, regardless of whether or not I'm actually connected to the internet, and whether or not the server software is running.
Thanks in advance.
The main problem here is a typo in your code; the line reading hostReachability = [Reachability reachabilityForInternetConnection]; in LoginViewController.m should be internetReachability = [Reachability reachabilityForInternetConnection];. The way it currently is, you never instantiate internetReachability, and then I guess [internetReachability currentReachabilityStatus] will always return nil.
Below is a basic example you can test with:
#interface LoginViewController ()
// ...
#property (strong, nonatomic) Reachability *hostReachability;
#property (strong, nonatomic) Reachability *internetReachability;
#property (strong, nonatomic) Reachability *wifiReachability;
#end
#implementation LoginViewController
// ...
- (void)setupReachabilityMonitoring
{
[NSNotificationCenter.defaultCenter addObserver:self
selector:#selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
_hostReachability = [Reachability reachabilityWithHostName:#"8.8.8.8"];
[_hostReachability startNotifier];
_internetReachability = [Reachability reachabilityForInternetConnection];
[_internetReachability startNotifier];
_wifiReachability = [Reachability reachabilityForLocalWiFi];
[_wifiReachability startNotifier];
[self logReachabilityStatus];
}
- (void)reachabilityChanged:(NSNotification *)notification
{
if ([notification.object isKindOfClass:Reachability.class]) {
[self logReachabilityStatus];
}
}
- (void)logReachabilityStatus
{
NSString *hostStatus = _hostReachability.currentReachabilityStatus ? #"up" : #"down";
NSString *internetStatus = _internetReachability.currentReachabilityStatus ? #"up" : #"down";
NSString *wifiStatus = _wifiReachability.currentReachabilityStatus ? #"up" : #"down";
NSLog(#"Internet is %#, wifi is %#, host is %#", internetStatus, wifiStatus, hostStatus);
}
#end
I am using this libarary using blocks so its doest not freeze the app and works in background
https://github.com/tonymillion/Reachability
I think the problem is in your "host name". It's not your server's IP, but your host name, e.g www.apple.com. You may check Apple's example code. At line 87, they set the host name to #"www.apple.com".

How to use Reachability in xcode for assessment Internet Connection

Im new in iphone and I want know that how to use Reachability in xcode.
I go on to Reachability example and read about it but understand about it.
I create one application and put Reachability.m and Reachability.h in it but I dont know how to use from it.
please guide me. I want when run application check my network Connection any time and run this code :
if (isConnection)
{
NSLog(#"Connection Success")
}
else
NSLog(#"Connection has been lost")
You can do this:
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
And now check the internetStatus var, by inspecting its value. The values are defined as:
typedef enum
{
// Apple NetworkStatus Compatible Names.
NotReachable = 0,
ReachableViaWiFi = 2,
ReachableViaWWAN = 1
} NetworkStatus;
So, in your case:
if (internetStatus == NotReachable)
{
NSLog(#"Bazinga!");
}
else
{
NSLog(#"Houston we have ignition");
}
Download Reachability Classes and Follow this code
internetReach = [[Reachability reachabilityForInternetConnection] retain];
[internetReach startNotifier];
Then we’ll set the NetworkStatus variable created in Reachability.
NetworkStatus netStatus = [internetReach currentReachabilityStatus];
And finally we’ll use the netStatus in a switch block.
switch (netStatus)
{
case ReachableViaWWAN:
{
break;
}
case ReachableViaWiFi:
{
break;
}
case NotReachable:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"We are unable to make a internet connection at this time. Some functionality will be limited until a connection is made." delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
break;
}
}
- (void) reachabilityChanged: (NSNotification* )note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus netStatus = [curReach currentReachabilityStatus];
switch (netStatus)
{
case ReachableViaWWAN:
{
break;
}
case ReachableViaWiFi:
{
break;
}
case NotReachable:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"We are unable to make a internet connection at this time. Some functionality will be limited until a connection is made." delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
break;
}
}
}

Reachability + UIAlertView + false-positive

Hey guys i'm having a few problems with Apple's reachability code.
What i've found out is that, even if the device is correctly connected to the internet, initially the reachability code will send out 1 false notification(Networkstatus = NotReachable) followed by a couple of correct notifications (Networkstatus = ReachableViaWiFi).
Therefore, as i am displaying an UIAlertView when i get a "NotReachable" notification, even if the device is connected to the internet, the app still outputs an uialertview informing the user that the device is not connected.
Is there anyway to avoid this inconvenience?
Any help would be really appreciated.
This is my code:
In my .h file:
#property (nonatomic, retain) Reachability *hostReach;
In my .m file:
- (void)viewDidLoad
{
self.hostReach = [Reachability reachabilityWithHostname:#"www.google.com"];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
[_hostReach startNotifier];
NetworkStatus netStatus = [self.hostReach currentReachabilityStatus];
if(netStatus == NotReachable && _alertShowing==NO){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"No internet connection found"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
_alertShowing = YES;
[alert show];
}
...
}
-(void)reachabilityChanged:(NSNotification *)note {
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus netStatus = [curReach currentReachabilityStatus];
if(netStatus == NotReachable && _alertShowing==NO){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"No internet connection found"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
_alertShowing = YES;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
_alertShowing = NO;
}
Why do you use reachabilityWithHostname:#"www.google.com"? This method checks the reachability of particular host (in your case google.com). And you receive notifications if Google available or not. Google may block you and you will receive NotReachable status.
Try to use:
//reachabilityForInternetConnection- checks whether the default route is available.
// Should be used by applications that do not connect to a particular host
+ (Reachability*) reachabilityForInternetConnection;
And also take a look on methods description here.

iPhone application freeze when out of range from wifi connection?

i have developed a radio application which uses network connection for online streaming, and also i am checking the condition whether network is available or not. if no network connection, it shows an alert "their is no network available" .my code is here
- (void)viewDidLoad
{
[super viewDidLoad];
//checking network reachability statys, this will show one alert view if no network available
Reachability* reachabile = [Reachability reachabilityWithHostName:#"www.apple.com"];
NetworkStatus remoteHostStatus = [reachabile currentReachabilityStatus];
if(remoteHostStatus == NotReachable)
{
NSLog(#"not reachable");
UIAlertView *notReachableAlert1=[[UIAlertView alloc]initWithTitle:#"NO INTERNET CONNECTION" message:#"This Application Need Internet To Run" delegate:self cancelButtonTitle:#"Okay Buddy" otherButtonTitles:nil];
notReachableAlert1.delegate=self;
[notReachableAlert1 show];
[notReachableAlert1 release];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerPlaybackStateDidChange:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:player];
// Do any additional setup after loading the view from its nib.
}
also i checked the condition on notifications
-(void) moviePlayerPlaybackStateDidChange:(NSNotification*)notification
{
Reachability* reachabile = [Reachability reachabilityWithHostName:#"www.apple.com"];
NetworkStatus remoteHostStatus = [reachabile currentReachabilityStatus];
NSLog(#"playbackDidChanged");
MPMoviePlayerController *moviePlayer = notification.object;
player=notification.object;
MPMoviePlaybackState playbackState = moviePlayer.playbackState;
if(playbackState == MPMoviePlaybackStateStopped)
{
NSLog(#"MPMoviePlaybackStateStopped");
}
else if(playbackState == MPMoviePlaybackStatePlaying) {
NSLog(#"MPMoviePlaybackStatePlaying");
} else if(playbackState == MPMoviePlaybackStatePaused) {
NSLog(#"MPMoviePlaybackStatePaused");
if(remoteHostStatus == NotReachable)
{
NSLog(#"not reachable");
UIAlertView *notReachableAlert1=[[UIAlertView alloc]initWithTitle:#"NO INTERNET CONNECTION" message:#"This Application Need Internet To Run" delegate:self cancelButtonTitle:#"Okay Buddy" otherButtonTitles:nil];
notReachableAlert1.delegate=self;
[notReachableAlert1 show];
[notReachableAlert1 release];
}
} else if(playbackState == MPMoviePlaybackStateInterrupted)
{
NSLog(#"MPMoviePlaybackStateInterrupted");
if((remoteHostStatus == NotReachable)&&(remoteHostStatus != ReachableViaWiFi))
{
NSLog(#"not reachable");
UIAlertView *notReachableAlert1=[[UIAlertView alloc]initWithTitle:#"NO INTERNET CONNECTION" message:#"This Application Need Internet To Run" delegate:self cancelButtonTitle:#"Okay Buddy" otherButtonTitles:nil];
notReachableAlert1.delegate=self;
[notReachableAlert1 show];
[notReachableAlert1 release];
}
my problem is, when app goes out of range from wifi connection without without 3g and normal data connection, it freezes for some time. and when i came back to range only it goes to active state and shows alert.
is their anything wrong i did with network availability checking?
this is sample u can edit based on ur application
#class Reachability;
#interface urAppDelegate : NSObject <UIApplicationDelegate>
{
Reachability* internetReachable;
Reachability* hostReachable;
BOOL hostActive;
BOOL internetActive;
}
#property (nonatomic, assign) BOOL hostActive;
#property (nonatomic, assign) BOOL internetActive;
#end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.internetActive=NO;
self.hostActive=NO;
[[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.apple.com"] retain];
[hostReachable startNotifier];
// now patiently wait for the notification
[self.window makeKeyAndVisible];
return YES;
}
-(void) checkNetworkStatus:(NSNotification *)notice{
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;
}
}
if (internetActive && hostActive)
{
// Net work Available.......
}
else
{
UIAlertView *netWorkAlert=[[UIAlertView alloc]initWithTitle:#"Network Connection Error" message:#"Please Check Connection" delegate:nil cancelButtonTitle:#"Close" otherButtonTitles:nil, nil];
[netWorkAlert show];
[netWorkAlert release];
}
}

Resources