Phillip
Phillip

Reputation: 4306

Reachability - Host appears down while Internet is reconnecting

I used this question to check whether the internet connection (WiFi \ 3G) is active.

It's working pretty good, but there's a small problem.

When I turn off the internet, the app gives the alert that Internet is down; but when i turn it on again, until the connection is definitely up, it says that the host is down while actually is up and in fact, after a while, it logs that is up.

I would like to know what could i do to let that message appear only when the server is actually down and not while reconnecting to the internet!

Here's my code

-(void) checkNetworkStatus:(NSNotification *)notice
{
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    if (internetStatus == NotReachable){
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Reloading Failed" message: @Seems like you're offline! \n Some features, such as updating contents and downloading screenshots won't be available while you're offline.\nNavigate to Settings > Wi-Fi to connect to the Internet and enjoy those features!") delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
        [alert show];
        [alert release];
        NSLog(@"offline");
    }
    else if (internetStatus == ReachableViaWiFi || internetStatus == ReachableViaWWAN){
        NSLog(@"online");
        if (hostStatus == NotReachable)
        {   NSLog(@"Server offline");
            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Reloading Failed" message:@"Seems like there's a communication problem with the remote network. \n Please try again in a while!" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
            [alert show];
            [alert release];
        }
        else if (hostStatus == ReachableViaWWAN || hostStatus == ReachableViaWiFi){
            NSLog(@"server online");
           //download data from remote server 
        }
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
    internetReachable = [[Reachability reachabilityForInternetConnection] retain];
    [internetReachable startNotifier];
    hostReachable = [[Reachability reachabilityWithHostName: @"www.mywebsite.com"] retain];
    [hostReachable startNotifier];

Any ideas?

Upvotes: 2

Views: 679

Answers (1)

Joe
Joe

Reputation: 57179

Reachability only serves the purpose of checking to see if the network is reachable and will not tell you if your server is down. You will need to use a combination of Reachability and an NSURLConnection or other 3rd party library to check for HTTP status 200 and other valid statuses. You will use Reachability to display a message that the device does not have connectivity and once you have connectivity you will use the web request to notify the user if the server is online or not. There is not much you can do about the connectivity delay after turning the internet back on for your device because it takes time to turn on the radios and connect to cell netowrks or wifi.

Upvotes: 3

Related Questions