ASIHTTPRequest Reachability As

As far as I like ASIHTTPRequest, it is not documented anywhere how to use the modified Reachability class, and I could not find it in stackoverflow or in any sample projects.

Im currently at this point:

Reachability *reach = [Reachability reachabilityWithHostName:@"http://google.com"]; [reach startNotifier]; if ([reach isReachable]) { NSLog(@"connection"); }else{ NSLog(@"no connection"); } 

This does not work.

+4
source share
2 answers

To do this, you need to configure the notification handler:

 Reachability *reach = [Reachability reachabilityWithHostName:@"http://google.com"]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; [reach startNotifier]; 

Then we implement the handler as follows:

 - (void) reachabilityChanged:(Reachability *) reach { if ([reach isReachable]) { NSLog(@"connection"); } else{ NSLog(@"no connection"); } } 

Also, when you don’t need to know when something is changing, remove yourself as an observer:

 [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil]; 

+6
source
 - (void) reachabilityChanged:(NSNotification *)notification { Reachability *localReachability = [notification object]; if ([localReachability isReachable]) { NSLog(@"connection"); } else{ NSLog(@"no connection"); } 

}

please make these changes, then the code will work like a charm :)

+1
source

All Articles