16

Downloaded Reachability from Apple, using this method to check for an active connection:

-(BOOL)isReachable{

    Reachability *r = [Reachability reachabilityWithHostName:@"http://www.google.com"];

    if(NotReachable == [r currentReachabilityStatus]) {
        NSLog(@"Not Reachable");
        return NO;
    }

    NSLog(@"Reachable");
    return YES;  

}

Returns NO every single time despite being connected? Can't figure out why...

Any ideas? Thanks.

On a side note, can anyone recommend a good clean Reachability singleton class?

Adam Waite
  • 19,175
  • 22
  • 126
  • 148

6 Answers6

30

EDITED: you should remove the protocol, http from your reachabilityWithHostName call, so updated it to

 Reachability *reachability = [Reachability reachabilityWithHostname:@"www.google.com"];
 NetworkStatus netStatus = [reachability currentReachabilityStatus];
Community
  • 1
  • 1
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
  • 1
    Yeh I've already done that and have the notification bits working fine. I want a method that checks the reachability state before doing certain tasks (such as downloading in-app-purchase products) – Adam Waite Jul 01 '12 at 17:57
  • @AdamWaite sorry i made a mistake before, i have updated the question – Omar Abdelhafith Jul 01 '12 at 17:59
  • 1
    I tried all solutions in this question and also multiple other questions. None of them do what I need. My problem is: After turning ON Airplane mode, it takes 5 seconds for Reachability to reflect the correct status of NotReachable. This happens if I subscribe to its events, or just try to query for the status directly. – FranticRock Sep 23 '15 at 19:21
5

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);
}
Nithinbemitk
  • 2,710
  • 4
  • 24
  • 27
2

I have a same issue with the Tony Million's Reachability : network status was always set to NotReachable. I fix it with adding the SystemConfiguration Framework

Hope it helps

Karine
  • 581
  • 1
  • 6
  • 20
1

I use KSReachability. It works with NSNotification, blocks, KVO, and with or without ARC.

KSReachability *reachability = [KSReachability reachabilityToHost:@"www.google.com"];
reachability.onReachabilityChanged = ^(KSReachability* reachability) {
    NSLog(@"Update: network is %@", reachability.reachable ? @"up." : @"down.");
};
Jano
  • 62,815
  • 21
  • 164
  • 192
1

If you are trying to see if the device can reach the internet in general you should probably use reachabilityForInternetConnection instead of reachabilityWithHostName:. Also, both of these calls will take a little bit of time to start up (it will still be in the milliseconds but longer than the time it takes to reach the if condition on the next line.) Here's an example of a singleton class that uses reachability.

static NetworkManager* sharedInstance = nil;

@interface NetworkManager()
@property (nonatomic, strong) Reachability* reachability;
@end

@implementation NetworkManager
@synthesize reachability;

+ (NetworkManager*)sharedInstance
{
    @synchronized(self) {
        if (sharedInstance == nil) {
            sharedInstance = [[NetworkManager alloc] init];
        }
    }
    return sharedInstance;
}

- (id)init
{
    reachability = [WYReachability reachabilityForInternetConnection];
}

- (BOOL)isNetworkReachable
{
    return ([self.reachability currentReachabilityStatus] != NotReachable);
}
@end

To check for the network reachable in other classes you can use.

[[NetworkManager sharedInstance] isNetworkReachable];
0

Another complete answer:

-(BOOL) isReachable:(NSString *)url
{
    //Retrieve the host name by given url address.
    NSString *hostName = [[NSURL URLWithString:url] host];
    Reachability *r = [Reachability reachabilityWithHostName:hostName];

    if(NotReachable == [r currentReachabilityStatus]) {
        NSLog(@"Not Reachable");
        return NO;
    }
    NSLog(@"Reachable");
    return YES;
}
gogoqaz
  • 182
  • 2
  • 10
  • It is working fine at first time. I need to check host reachability on each action. But, always return Reachable via Wifi. Not the host reachable or not. – Vineesh TP Mar 15 '17 at 03:15
  • as per Reachbilty class file of Apple, `Reachability cannot tell your application if you can connect to a particular host, only that an interface is available that might allow a connection, and whether that interface is the WWAN.` [Apple Reachbilty](https://developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007324-Intro-DontLinkElementID_2) – Anil Gupta Jun 01 '17 at 08:54