Reachability host not reachable no matter which host I use - ios

This case (NSLog(#"A gateway to the host server is down.");
is always running from some reason.
I'm using Apple Reachability class behind the scene.
I tried to insert other hosts but no luck please help.
Thanks in advance.
Here is the code
#implementation ConnectionManager
#synthesize internetActive, hostActive;
-(id)init {
self = [super init];
if(self) {
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(checkNetworkStatus:) name:#"NetworkReachabilityChangedNotification" object:nil];
internetReachable = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];
hostReachable = [Reachability reachabilityWithHostName:#"www.google.com"];
[hostReachable startNotifier];
return self;
}
- (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;
}
}
}

Since you haven't mentioned about internetReachable showing the same error, I am assuming that you are able to connect to internet and that part is working fine. For the hostReachable part, you can try to change it as:
Change
[Reachability reachabilityWithHostName:#"www.google.com"];
to
[Reachability reachabilityWithHostName:#"http://www.google.com"];

Related

iOS really check internet connection

I have one problem - i need to check really internet connection. What i do now? Now i use Reachability class to check if is internet connection, but my app go to crash in this case: I've connected to the wifi in airport, it's was free wi fi but if i am not registred i can't surf. But Reachability thinkin that i've internet connection.How i use Reachability now?
if(self.internetReachableFoo.isReachable)
{
// We have internet
}
else
{
//we have no internet
}
So my question is, how i can use Reachability or other tools to check real internet connection?
thanks.
I you wan't to check internet connectivity then you can use reachability block
Reachability *reachability = [Reachability reachabilityWithHostname:#"www.google.com"];
reachability.reachableBlock = ^(Reachability *reachability) {
NSLog(#"Network is reachable.");
};
reachability.unreachableBlock = ^(Reachability *reachability) {
NSLog(#"Network is unreachable.");
};
// Start Monitoring
[reachability startNotifier];
You could attempt to connect to services you know will always be available, such as Google's public DNS servers. A simple ping will suffice.
I'm using this solution:
#implementation SomeClass{
Reachability* hostReachable;
}
Add this to your viewDidLoad method
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
hostReachable = [Reachability reachabilityWithHostName:#"www.apple.com"];
[hostReachable startNotifier];
And this method:
-(void) checkNetworkStatus:(NSNotification *)notice
{
NetworkStatus hostStatus = [self.hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
NSLog(#"NotReachable");
break;
}
case ReachableViaWiFi:
{
NSLog(#"ReachableViaWiFi");
break;
}
case ReachableViaWWAN:
{
NSLog(#"ReachableViaWWAN");
break;
}
}
}

reachability alway return NotReach

i use reachability to detect network change. the first time its ok with wifi available. but then i turn off wifi then turn on, it alway return NotReach
Reachability *internetChecker;
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(myMethod:) name:kReachabilityChangedNotification object:nil];
internetChecker = [Reachability reachabilityWithHostName:#"www.24h.com.vn"];
[internetChecker startNotifier];
in mymethod:
NetworkStatus status = [internetChecker currentReachabilityStatus];
switch (status) {
case NotReachable:
{
NSLog(#"no wifi");
break;
}
default:
{
NSLog(#"wifi");
break;
}
}
Do you have separate Reachability properties for the Internet and the Host?
#property (nonatomic) Reachability *internetReachability;
#property (nonatomic) Reachability *hostReachability;
Then do this:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMethod:) name:kReachabilityChangedNotification object:nil];
self.internetReachability = [Reachability reachabilityForInternetConnection];
[self.internetReachability startNotifier];
[self updateInterfaceWithReachability:self.internetReachability];
self.hostReachability = [Reachability reachabilityWithHostName:#"www.24h.com.vn"];
[self.hostReachability startNotifier];
[self updateInterfaceWithReachability:self.hostReachability];
Then myMethod:
- (void)myMethod:(NSNotification *)note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
[self updateInterfaceWithReachability:curReach];
}
And updateInterfaceWithReachability:
- (void)updateInterfaceWithReachability:(Reachability *)reachability {
NetworkStatus netStatus = [reachability currentReachabilityStatus];
if (reachability == self.internetReachability) {
switch (netStatus) {
case NotReachable:
NSLog(#"no internet");
break;
case ReachableViaWWAN:
case ReachableViaWiFi:
NSLog(#"with internet");
break;
}
}
if (reachability == self.hostReachability) {
switch (netStatus) {
case NotReachable:
NSLog(#"www.24h.com.vn unavailable");
break;
case ReachableViaWWAN:
case ReachableViaWiFi:
NSLog(#"www.24h.com.vn available");
break;
}
}
Try below solution
+ (BOOL)checkInternetConnection
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
if (remoteHostStatus == NotReachable)
{
return FALSE;
}
else
{
return TRUE;
}
}
you have to modify this line
Reachability * internetChecker = [Reachability reachabilityForInternetConnection];
Hope it helps you..!
done! i dont know how but its seem notification center only detect when network change. When it notice the status is still NotReachable. So i check for the network after 2s delay. It work!

Parse API always Unreachable?

I'm using Apple's Reachability framework, "api.parse.com" is ALWAYS Unreachable. Am I missing something?
In my viewDidLoad, I have this:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
self.hostParseReachability = [Reachability reachabilityWithHostName:kParseConnectivityTestURL];
[self.hostParseReachability startNotifier];
[self updateInterfaceWithReachability:self.hostParseReachability];
Then I have the following Reachability methods:
-(void)reachabilityChanged:(NSNotification* )note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
[self updateInterfaceWithReachability:curReach];
}
- (void)updateInterfaceWithReachability:(Reachability *)reachability {
NetworkStatus netStatus = [reachability currentReachabilityStatus];
if (reachability == self.internetReachability) {
switch (netStatus) {
case NotReachable:
isConnectedToInternet = NO;
break;
case ReachableViaWWAN:
case ReachableViaWiFi:
isConnectedToInternet = YES;
break;
}
}
if (reachability == self.hostParseReachability) {
switch (netStatus) {
case NotReachable:
isParseOnline = NO;
break;
case ReachableViaWWAN:
case ReachableViaWiFi:
isParseOnline = YES;
break;
}
}
Parse is ALWAYS offline for me, but I can perform queries.
They have a firewall blocking ICMP packets. If you need to check manually if everything is fine - you can try curl
If parse is unreachable, you'll get kPLParseConnectionFailed (in case of iOs app).

Internet connectivity returns false for cellular networks on iphone(xamarin.ios)

The following code returns true for wifi connection but false while checking for cellular(wwan) network on device ,
here is the code
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com");
request.Timeout = 25000;
request.Credentials = CredentialCache.DefaultNetworkCredentials;
request.UseDefaultCredentials=true;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return response.StatusCode == HttpStatusCode.OK;
}
catch (Exception e)
{
return false;
}
i am getting the error as
The remote server returned an error: (403) Forbidden
help out.
Check out Xamarin Reachability class here.
Edit:
Download and install the vodafone profile from http://db.tt/SqQGQ9Ci
You can use Reachibility
#import "Reachability.h"
+(bool)internetConnection
{
Reachability* reachability;
reachability = [Reachability reachabilityWithHostname:#"www.google.com"];
NetworkStatus netStatus = [reachability currentReachabilityStatus];
[reachability startNotifier];
switch (netStatus)
{
case NotReachable:
{
//[self showLoadingView:#"Internet Unavailable!!"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"NetworkFail" object:self];
return NO;
break;
}
case ReachableViaWWAN:
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"NEtworkPass" object:self];
return YES;
break;
}
case ReachableViaWiFi:
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"NEtworkPass" object:self];
return YES;
break;
}
}
}

Trigger action immediately after connection is available

I want to trigger an action once the connection is available. There are solutions available which allows manually checking internet connection. One way i find is using NSTimer to check for internet connection during fixed intervals. But is it the most effective way to check for it? if Not, What is the right solution for this?
Here how you can register the observer and listen to it, Your application will be listening to kReachabilityChangedNotification & prompt you whenever status of network changes.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reachabilityHasChanged:) name:kReachabilityChangedNotification object:nil];
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
-(void) reachabilityHasChanged:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
NSLog(#"The internet is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(#"The internet is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(#"The internet is working via WWAN.");
break;
}
}
}
Check reachability code provided by apple.In appdelegate.m you can see this method.It will notify the network change.Work on it
//Called by Reachability whenever status changes.
- (void) reachabilityChanged: (NSNotification* )note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
[self updateInterfaceWithReachability: curReach];
}

Resources