tony million Reachability says unreachable when connected
Asked Answered
L

1

10

I have searched but have not found an issue like mine. I'm sure it's something I have over looked .

I am using tony million's reachability block method. it is working good when i have internet then no internet. the alert comes up and works just fine.

but, when i have no internet and then i get internet the same alert pops up

my code is

-(void)reachabilityBlock
{
// allocate a reachability object
Reachability * reach = [Reachability reachabilityWithHostname:@"www.google.com"];

// tell the reachability that we DONT want to be reachable on 3G/EDGE/CDMA
reach.reachableOnWWAN = YES;


reach.reachableBlock = ^(Reachability * reachability)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        //NSLog(@"REACHABLE! block");
        [self newsTableViewRefresher];
    });
};

reach.unreachableBlock = ^(Reachability * reachability)
{

        dispatch_async(dispatch_get_main_queue(), ^{
            //NSLog(@"UNREACHABLE! block");

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"No Network found!"
                                                            message: @"You have no wifi or cellular connection available. Please connect to a WIFI or cellular network."
                                                           delegate: self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];

            [alert show];


        });

};


[reach startNotifier];
[self.refreshControl endRefreshing];

 }

my question is why when i get internet does the unreachable alert pop up?

thank you for your time

Lely answered 14/10, 2013 at 19:3 Comment(2)
Try checking the reachability parameter in the unreachable block before showing the alert. Also, log it out to see which block is getting called first.Subtitle
@jgervin I'm getting better results using [Reachability reachabilityForInternetConnection]; instead of [Reachability reachabilityWithHostname:@"www.google.com"];Arrowhead
K
2

This is what I did on my end and got the job done for me. I try using the blocks but it seems it was more trouble that solving my issue. Hope this helps.

Reachability *reach = [Reachability reachabilityWithHostname:@"www.jportdev.com"];
if ([reach isReachable]){
    // Reachable
    //NSLog(@"is reachable.......");
}else{
    // not Reachable
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network is unavaliable!" message:@"Some content in this application might not be avaliable without network connectivity." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    alert = nil;
}
Kurtz answered 1/11, 2013 at 1:58 Comment(2)
ok but then when you loss the connection how are you going back to this if whiteout the blocks?Pibgorn
For me even the isReachable is reporting the incorrect value (when going from offline to online) when the network is available and the target address is reachable. I'm using the iOS simulator though, so not sure if this is a supported way of working.Hellenic

© 2022 - 2024 — McMap. All rights reserved.