I have implemented Reachability Api 2.2. When the network goes from an off state to an on state it does not fire.
In addition, can I implement it in the app delegate? If so, where should I remove observer?
Here is my code (which does not call dismiss model viewController)
- (void)viewDidLoad
{
// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [[Reachability reachabilityForInternetConnection] retain] ;
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName: @"www.google.com"] retain];
[hostReachable startNotifier];
}
- (void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus hoststatus=[hostReachable currentReachabilityStatus];
NetworkStatus internetStatus=[internetReachable currentReachabilityStatus];
for (NSString *msg in messageArray) {
[stringg appendString:[NSString stringWithFormat:@"%@",msg]];
}
if (hoststatus==NotReachable||internetStatus==NotReachable) {
[self.navigationController presentModalViewController:inter animated:YES];
}
else{
[self dismissModalViewControllerAnimated:YES];
}
[inter release];
}
- (void)viewDidUnload
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super viewDidUnload];
}
Where are you registering for notifications?
You need something like this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChange:)
name:kReachabilityChangedNotification
object:nil];
I use the reachability object passed as a parameter like this:
- (void) reachabilityChange: (NSNotification*) notification
{
Reachability* curReach = [notification object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
BOOL isServerCurrentlyReachable = (NotReachable != [curReach currentReachabilityStatus]);
BOOL wasServerPreviouslyReachable = self.isServerReachable;
self.isServerReachable = isServerCurrentlyReachable;
if (NO == wasServerPreviouslyReachable && YES == isServerCurrentlyReachable)
{
// Moving from non reachable to reachable state
}
else if (YES == wasServerPreviouslyReachable && NO == isServerCurrentlyReachable)
{
// Moving from a reachable to a non reachable state
}
}
You don't seem to be using that.
Also the notification can get called several times with the same status, so you need to make sure this is accounted for, as done in the snippet.
If you're using it in the app delegate, stopping/removing stuff in the applicationDidResignActive would seem to be the appropriate place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With