iOS Network Reachability - Doesn't Seem to Be Working

iOS network reachability - doesn't seem to be working

See the SCNetworkReachability Reference.

The SCNetworkReachability programming
interface allows an application to
determine the status of a system's
current network configuration and the
reachability of a target host. A
remote host is considered reachable
when a data packet, sent by an
application into the network stack,
can leave the local device.
Reachability does not guarantee that
the data packet will actually be
received by the host.

Turning IIS on and off is just preventing your server from receiving web request such as ftp/http and does not stop the device from successfully sending a data packet out.

Testing the internet connection is not working with Reachability

Ok, so best way I got is following Apple sample code:

   //Test the internet connection
Reachability* reach = [Reachability reachabilityForInternetConnection];

NetworkStatus netStatus = [reach currentReachabilityStatus];
//See if the reachable object status is "ReachableViaWifi"
if (netStatus!=ReachableViaWiFi) {
//If not
NSLog(@"wifi unavailable");
//Alert the user about the Internet cnx
WBErrorNoticeView *notice = [WBErrorNoticeView errorNoticeInView:self.view title:@"Network Error" message:@"Check your internet connection."];
notice.sticky = NO;
[notice show];
return;//Exit the method
}

Reachability not working when wi-fi connected but no internet

ok, I found out the answer to this - Apple's reachability does not test actual connectivity to the host. See the answer by @Zhami in the SO link below:

How to write a simple Ping method in Cocoa/Objective-C

Essentially, when you first launch the app and do a reachability check, iOS seems to do a DNS lookup, and if there is no internet, the check fails. So the first time you check reachability , it actually returns a meaningful value. However, if you are conected at app launch, and lose internet connectivity after some time (while still connected to WiFi/3G/4G but no underlying internet connectivity), further reachability checks return reachable even though the internet or your specified host is not reachable anymore.

So if you want to really check for connectivity in real time, consider using the following:

-(BOOL) isConnected
{

NSString* url = [NSURL URLWithString:@"http://www.google.com/m"];
ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
[request setTimeOutSeconds:10];
//customize as per your needs - note this check is synchronous so you dont want to block the main thread for too long
[request setNumberOfTimesToRetryOnTimeout:0];
[request startSynchronous];

NSError *error = [request error];
if (error)
{
DLog(@"connectivity error");
return NO;
}
else
{
DLog(@"connectivity OK");
return YES;
}

}

Which is the best method to check the network reachability in objective c

I always prefer to use Reachability https://github.com/tonymillion/Reachability like below. Description on GitHub is so clear for why you should use it -> "This is a drop-in replacement for Apple's Reachability class. It is ARC-compatible, and it uses the new GCD methods to notify of network interface changes".

Reachability *internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];

// Internet is reachable
internetReachableFoo.reachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{

//do something
NSLog(@"REACHABLE!");

} error:^(NSError * _Nullable error) {
NSLog(@"Error:%@ ", [error localizedDescription]);
}];
});
};

// Internet is not reachable
internetReachableFoo.unreachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{

//do something
NSLog(@"UNREACHABLE!");

});
};

[internetReachableFoo startNotifier];

goodluck :)

Reachability class and hostnames that require https or vpn (ios)

You need to check reachability on a specific port (443 in this case). This is something that is not supported by Reachability API. Refer to this question, or this.

Relevant part from documentation:

A remote host is considered reachable when a data packet, sent by an
application into the network stack, can leave the local device.
Reachability does not guarantee that the data packet will actually be
received by the host.

So, basically Reachability doesn't test if the remote host accepts incoming connections. For this purpose, I suspect you'll need to open an URL connection and wait for time out/other errors.



Related Topics



Leave a reply



Submit