0

Hey guys i'm having a few problems with Apple's reachability code. What i've found out is that, even if the device is correctly connected to the internet, initially the reachability code will send out 1 false notification(Networkstatus = NotReachable) followed by a couple of correct notifications (Networkstatus = ReachableViaWiFi). Therefore, as i am displaying an UIAlertView when i get a "NotReachable" notification, even if the device is connected to the internet, the app still outputs an uialertview informing the user that the device is not connected.

Is there anyway to avoid this inconvenience?

Any help would be really appreciated.

This is my code:

In my .h file:

@property (nonatomic, retain) Reachability *hostReach;

In my .m file:

- (void)viewDidLoad
{
    self.hostReach = [Reachability reachabilityWithHostname:@"www.google.com"];

    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

    [_hostReach startNotifier];


    NetworkStatus netStatus = [self.hostReach currentReachabilityStatus];


    if(netStatus == NotReachable && _alertShowing==NO){

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                    message:@"No internet connection found"

                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles: nil];

        _alertShowing = YES;

        [alert show];

    }

    ...

}


-(void)reachabilityChanged:(NSNotification *)note {

    Reachability* curReach = [note object];

NSParameterAssert([curReach isKindOfClass: [Reachability class]]);    

    NetworkStatus netStatus = [curReach currentReachabilityStatus];


    if(netStatus == NotReachable && _alertShowing==NO){

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                    message:@"No internet connection found"

                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles: nil];

        _alertShowing = YES;

        [alert show];

    }


-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    _alertShowing = NO;

}

1 Answers1

1

Why do you use reachabilityWithHostname:@"www.google.com"? This method checks the reachability of particular host (in your case google.com). And you receive notifications if Google available or not. Google may block you and you will receive NotReachable status.

Try to use:

//reachabilityForInternetConnection- checks whether the default route is available.  
//  Should be used by applications that do not connect to a particular host
+ (Reachability*) reachabilityForInternetConnection;

And also take a look on methods description here.

EugeneK
  • 100
  • 4
  • Thanks alot @EugeneK, i think that solved it. But for what reason would Google block me for? Ps i was using reachabilityWithHostname:@"www.google.com" because in Apple's sample code they were using reachabilityWithHostname:@"www.apple.com" and i thought that google would have more reliable servers. – puntotuning Nov 12 '12 at 21:51
  • 1
    Well if google were to think you or your users was trying to DDOS them by sending a ton of packets to their servers, you might think twice about asking that kind of question. – CodaFi Nov 12 '12 at 22:27
  • Ok, i've understood your point of view @CodaFi. But if that was the case, why is apple using reachabilityWithHostname:@"www.apple.com in their reachability sample code? – puntotuning Nov 13 '12 at 18:17
  • No idea. Reachability is a dated beast of a control. Use NPReachability instead, it'll make your life easier. – CodaFi Nov 13 '12 at 21:58