3

I would like to do some treatment when i loose connection, and when connection is back. Are there any events to handle it ?

Thanks by advance,

E.

Spaceduck
  • 31
  • 2
  • You could take help of http://stackoverflow.com/questions/4501864/reachability-network-change-event-not-firing – Ravi Jan 13 '12 at 09:12

2 Answers2

0

You should use good practices used in ASIHTTPRequest. They use Reachability which is as they say a drop in replacement of the class develop by Apple I hope it will help

gsempe
  • 5,371
  • 2
  • 25
  • 29
0

One standard approach is to use Reachability to test network availability. It can be downloaded here. You only need Reachability.h and Reachability.m in your project.

My personal preference is to do the following -

1 Add the Reachability files

2 Create BOOL properties for each network test you wish to remember/expose in your project - I have a test for google and a test for google maps below.

3 In your appDidFinishLoading method call [self assertainNetworkReachability].

#pragma mark -
#pragma mark Reachability

-(void)assertainNetworkReachability {
    [self performSelectorInBackground:@selector(backgroundReachabilityTests)  withObject:nil];
}

-(void)backgroundReachabilityTests {

    self.isInternetReachable = [self internetReachable];
    self.isMapsReachable = [self mapsReachable];

    self.connectivityTimer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self     selector:@selector(backgroundReachabilityTests) userInfo:nil repeats:NO];
}

-(BOOL)hostReachable:(NSString*)host {
    Reachability *r = [Reachability reachabilityWithHostName:host];
    NetworkStatus internetStatus = [r currentReachabilityStatus];
    if(internetStatus == NotReachable) {
        [self throwNetworkDiagnosisAlert];
        return NO;
    }
    return YES;
}

-(BOOL)internetReachable {
    return [self hostReachable:@"www.google.co.uk"];
}

-(BOOL)mapsReachable {
    return [self hostReachable:@"maps.google.com"];
}

-(BOOL)isInternetGoodYetMapsUnreachable {
    return (self.isInternetReachable && !self.isMapsReachable);
}

-(void)throwNetworkDiagnosisAlert {
    NSString* title = @"Connectivity Problem";
    NSString* message = @"You are not connected to the internet.";

    if (self.isInternetGoodYetMapsUnreachable) {
        message = @"Unable to connect to the Google Maps server.";
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [alert show];
    [alert release];
}
Damo
  • 12,840
  • 3
  • 51
  • 62