Reachability not working as expected
Asked Answered
S

6

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?

Surgical answered 1/7, 2012 at 17:46 Comment(0)
R
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];
Replenish answered 1/7, 2012 at 17:53 Comment(3)
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)Surgical
@AdamWaite sorry i made a mistake before, i have updated the questionReplenish
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.Monoceros
M
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);
}
Monteverdi answered 3/1, 2013 at 5:41 Comment(0)
M
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

Marcille answered 4/8, 2014 at 9:37 Comment(1)
This helps solving my case, but this should be put in commentPsia
C
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.");
};
Clouet answered 1/7, 2012 at 18:4 Comment(0)
F
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];
Fiveandten answered 1/7, 2012 at 20:53 Comment(0)
R
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;
}
Rogovy answered 1/9, 2014 at 7:1 Comment(2)
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.Fricassee
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 ReachbiltyInterloper

© 2022 - 2024 — McMap. All rights reserved.