1

I want to know which is the latest and effective method to check internet connection in iOS.Most of the answers in stackoverflow is outdated it seems.I went through REACHABILITY class.It is working fine.But I have a problem.

I have a wifi connection which ask for provider login at times.So the reachability class shows that wifi connection is available however internet is not usable.

so,when I call a url,the activityIndicator that I am using along with the url keeps on spinning and I am getting no callback about the error.Is there a fix?Thanks.

Also,can you explain what these exactly means?

@property (nonatomic) Reachability *hostReachability;
@property (nonatomic) Reachability *internetReachability;
@property (nonatomic) Reachability *wifiReachability;

like,What is wifiReachability?Is it connected to wifi?or internet is available through wifi?

EDIT: This is what I am using now.Creating a class called connectivity

#import "Connectivity.h"
#import "Reachability.h"
@implementation Connectivity
+(BOOL)checkConnectivity
{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];
    return networkStatus != NotReachable;
}
@end

Is this enough to check internetconnection?

abhimuralidharan
  • 5,752
  • 5
  • 46
  • 70
  • 3
    I'm not sure (hence the comment), but I would *assume* `hostReachability` is whether or not the website can be reached, `internetRechability` is whether or not there is a internet connection (not necessarily a functional one), and `wifiReachability` is whether or not there is a connection to a wifi network (and not necessarily a working connection). – Jojodmo Oct 30 '15 at 05:06
  • 1
    @Jojodmo - Your assumption is correct. Just to add, `internetReachability` checks for the default route on network, which is basically nothing but checking if internet can be connected without hitting a particular host. You should post it as answer. – Gandalf Oct 30 '15 at 05:23
  • 1
    I don't think you cannot check the `real internet connection` with `Reachability`. I would use a specific `REST API` and check the response is as expected. – Ryan Oct 30 '15 at 05:39
  • That is a good trick! :) – abhimuralidharan Oct 30 '15 at 06:08
  • Abhi refer this http://stackoverflow.com/questions/1083701/how-to-check-for-an-active-internet-connection-on-iphone-sdk link – user3182143 Oct 30 '15 at 06:15

1 Answers1

1

Following is the another alternate very simple way to check internet connectivity:

NSURL *scriptUrl = [NSURL URLWithString:@"http://www.google.com/m"];
NSData *data = [NSData dataWithContentsOfURL:scriptUrl];
if (data)
    NSLog(@"Device is connected to the internet");
else
    NSLog(@"Device is not connected to the internet");

Hope this will help.

Rohan
  • 2,939
  • 5
  • 36
  • 65