Frage

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;

}
War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top