Is it possible to receive a notification or something inside my app when the internet BECOMES available. I'm aware of reachability and all kinds of stuff. But what i want is to start some pending NSUrlConnections when internet becomes available on the device. Is there a easy way to do this, because I don't want to use a looping thread that checks constantly for reachability. Any suggestions?
-
2From your question I don't understand why using Reachability and registering for ReachabilityChangedNotification s won't work. What is it you are looking for that Reachability doesn't offer? – Alex Nov 01 '13 at 19:11
-
1[Reachability](https://developer.apple.com/Library/ios/samplecode/Reachability/Introduction/Intro.html) really is worth another look. You shouldn't be looping, but rather, like that code sample, call `startNotifier` and you'll get a notification when the Internet connectivity changes. – Rob Nov 01 '13 at 19:15
-
I don't agree this question is duplicate. Answer provided is about using Reachability, but question is about getting connection status update. As I've written in my answer, Reachability don't guarantee anything, it's more like helper. Suggesting to anyone, that Reachability is an right answer may lead to unhanded states in application! – Maciej Kozieł Nov 02 '13 at 14:55
2 Answers
Ok, here is very nice post about Reachability: http://www.mikeash.com/pyblog/friday-qa-2013-06-14-reachability.html (check comments below!)
Tldr: You can fire block when your connection is back, but this solution is not perfect. There is no 100% reliable way to do this (except trying in loop), but you can try to mix those approaches.
Edit: Comment to @Jonah.at.GoDaddy answer:
Reachability can give you both connection notification errors: false positive and false negative (you can check it at WWDC 2011 session, I don’t remember which one; there are two about networking). So, my point is: you should never rely only on those notification. You can trigger refresh on status change, but there should be another way of doing it (user interaction, or some kind of active waiting).

- 959
- 11
- 24
Here is some code that I use...it may be a bit more than you need:
-(void)checkNetworkStatus
{
// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
// check if a pathway to a random host exists
self.hostReachable = [Reachability reachabilityWithHostname:@"google.com"];
[self.hostReachable startNotifier];
}
-(void) checkNetworkStatus:(NSNotification *)notice
{
NetworkStatus hostStatus = [self.hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
DDLogInfo(@"A gateway to the host server is down.");
if( self.canReachGoogle )
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: LOCALIZED_NoInternetConnection
message: LOCALIZED_ConnectionNeeded
delegate:self cancelButtonTitle:LOCALIZED_Ok otherButtonTitles:nil];
[alert show];
}
self.canReachGoogle = NO;
break;
}
case ReachableViaWiFi:
{
DDLogInfo(@"A gateway to the host server is working via WIFI.");
self.canReachGoogle = YES;
break;
}
case ReachableViaWWAN:
{
DDLogInfo(@"A gateway to the host server is working via WWAN.");
self.canReachGoogle = YES;
break;
}
}
DDLogInfo(@"Network connection has changed and is now: %@", self.canReachGoogle ? @"enabled" : @"disabled" );
}

- 3,066
- 2
- 18
- 28
-
Yeah but that is relying on Google. Not the best approach. And also I asked for changes in reachability, not for reachability at a certain time. – user2387149 Jan 10 '14 at 17:18