Reachability not working as expected

▼魔方 西西 提交于 2019-12-21 07:03:37

问题


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?


回答1:


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];



回答2:


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);
}



回答3:


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




回答4:


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.");
};



回答5:


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];



回答6:


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;
}


来源:https://stackoverflow.com/questions/11284302/reachability-not-working-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!