Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do you put your call to Reachability?

I'm trying to implement Reachability in my app. I have it in applicationDidFinishLaunching. If my connection is really bad and I launch the app, Reachability takes forever and often crashes the app with an The application failed to launch in time error message.

So instead, I tried putting the call to Reachability in a background thread. But when I do this, I no longer receive the Reachability notifications.

Where should I be calling Reachability?

Edit:

I added TonyMillions' Reachability code per the suggestion below, but I'm still getting the same application failed to load in time error when in very bad network conditions. To reproduce what I'm seeing, go to Settings.app -> Developer -> Network Link Conditioner, turn it on and set it to 100% loss.

Does your app still load in that condition?

like image 349
bmueller Avatar asked Nov 03 '22 12:11

bmueller


1 Answers

Use Tony Millions reachability project on Github.

https://github.com/tonymillion/Reachability

Then in my app delegate I just put in applicationdidfinishlaunching

self.reachability = [Reachability reachabilityForInternetConnection];

[self.reachability startNotifier];

Then when the status changes this will be called

#pragma mark - Reachability

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

    NetworkStatus ns = [(Reachability *)[note object] currentReachabilityStatus];

    if (ns == NotReachable) {

        if (![self.networkAlert isVisible]) {

            if ([self networkAlert] == nil) {

                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"\n\nNo Internet Connection" message:@"You require an internet connection to communicate with the server." delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
                [self setNetworkAlert:alert];
            }

            [self.networkAlert show];  
        }
    } else {

        if ([self networkAlert] != nil) {

            [self.networkAlert dismissWithClickedButtonIndex:0 animated:YES]; 
        }
    }
}
like image 132
Hackmodford Avatar answered Nov 15 '22 05:11

Hackmodford