Biranchi
Biranchi

Reputation: 16327

Reachability 2.0 in iPhone

I am using Reachability v2.0 in my app.

I have a valid internet connection and able to browse google page in web browser. When i am trying to test the reachability status in my App, its always showing "Host is not Reachable".

    NSString *host = @"http://www.google.co.in";

    NSLog(@"host : %@", host);
    Reachability *hostReach = [Reachability reachabilityWithHostName:host];
    if([hostReach currentReachabilityStatus] == NotReachable) {
        NSLog(@"Host is not Reachable");
    } else {
        NSLog(@"Host is reachable");
    }

What is wrong in the above code ??

Upvotes: 1

Views: 806

Answers (2)

ExplodingPhone
ExplodingPhone

Reputation: 31

The problem is that the reachabilityWithHostname method is expecting a hostname, not a URL as you've defined.

You need to define the host as :-

NSString *host = @"www.google.co.in";

Upvotes: 3

Jens Bergvall
Jens Bergvall

Reputation: 1627

Try implementing this on any function that requires internet connection!

#import "testConnectionViewController.h"
#import "Reachability.h"

@implementation testConnectionViewController




- (void)doWeHasInternets{

  UIAlertView *errorView;
  Reachability *r = [Reachability reachabilityWithHostName:@"m.google.com"];
  NetworkStatus theInternets = [r currentReachabilityStatus];


  if(theInternets == NotReachable) {
    errorView = [[UIAlertView alloc]
                 initWithTitle: @"Network Error"
                 message: @"The internets are down, everyone run for your lives!"
                 delegate: self
                 cancelButtonTitle: @"Nooooooo!" otherButtonTitles: nil];    
  }
  else
  {
    errorView = [[UIAlertView alloc]
                 initWithTitle: @"Whew!"
                 message: @"Relax, the internets are here."
                 delegate: self
                 cancelButtonTitle: @"Yay!" otherButtonTitles: nil];    
  }

  [errorView show];
  [errorView autorelease];
}

@end

Upvotes: 0

Related Questions